2017-03-18 19 views
1

文字列の一部にならないように疑問符を避けるにはどうすればよいでしょうか?文字列の疑問符の回避

たとえば、ここでの戻り値は7でなければなりませんが、「スープ」を考慮しないため6が返されます。どのように疑問符を避けることが可能でしょうか?どんな助けもありがとうございます。

function timedReading (maxLength, text) { 
var sep = text.split(" "); 
result = 0; 
count = 0; 
for(var i = 0; i < sep.length; i++) { 
    if(sep[i].length<=maxLength) { 
    result += sep[i]; 
    count++; 
    } 
} 
return count; 
} 
timedReading(4,"The Fox asked the stork, 'How is the soup?'"); 
+0

あなたは交換し、[置き換える](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)文字列を使用することができますそれらは空文字列である。 – abhishekkannojia

+0

あなたが何もしたくないものだけを交換してください。たとえば、アルファベットとスペース - > 'str.replace(/ [^ a-zA-Z] /、 '')' – adeneo

答えて

2

特殊文字から各単語をフィルタリングします。

function timedReading(maxLength, text) { 
 
    var sep = text.split(" "); 
 
    result = 0; 
 
    count = 0; 
 
    for (var i = 0; i < sep.length; i++) { 
 
    if (sep[i].split('').filter(v => !/[^A-za-z0-9]/.test(v)).join('').length <= maxLength) { 
 
     result += sep[i]; 
 
     count++; 
 
    } 
 
    } 
 
    console.log(count); 
 
} 
 
timedReading(4, "The Fox asked the stork, 'How is the soup?'");

関連する問題