-2

假设我有一个包含“你好,世界!”的字符串。另一个带有“hello world”的函数,我希望我的函数对第一个返回false,对第二个返回trye,基本上是根据一组有效字符检查它,例如az。

问题是我不想使用正则表达式。我可以使用 .indexOf 但它只适用于一个字符。

有什么方法可以做到这一点,可能是非阻塞方式(使用node.js)?

提前致谢。

在 PHP 中,我曾经做过类似的事情:

$sUser = 'my_username01'; 
$aValid = array('-', '_'); 

if(!ctype_alnum(str_replace($aValid, '', $sUser))) { 
  echo 'Your username is not properly formatted.'; 
} 
4

1 回答 1

1
function hasBadChars(s) {
    var badChars = ['!'],
        i;

    for (i = 0; i < s.length; ++i) {
        if (badChars.indexOf(s[i])) {
            return true;
        }
    }
    return false;
}

console.log(hasBadChars('Hello world!'));
于 2013-09-11T13:28:29.137 回答