function longestWord(string) {
var str = string.split(" ");
var longest = 0;
var word = null;
for (var i = 0; i < str.length; i++) {
var checkedLetters = "";
for (var j = 0; j < str[j].length; j++) {
if (j == /[^a-zA-Z]/) {
checkedLetters += j;
}
if (longest < checkedLetters.length) {
longest = checkedLetters.length;
word = checkedLetters;
}
}
}
return word;
}
我使用正则表达式有什么问题吗?当我调用longestWord("Hello, I am here") 时,我希望它返回"Hello"(不带逗号),但它返回null。