我熟悉寻找一个给定的子字符串:如何在javascript中的字符串中搜索一个单词(不只是一个子字符串)?
if (string.indexOf(substring) > -1) {
var containsSubstring = true;
}
但是,如果子字符串必须一个字?
一个字:
- 它必须与后一个空格字符串的开头;或
- 在字符串的末尾加上一个空格;或
在字符串中间两边各有
空间
fox
: the quick brown fox // matches
fox jumps over the lazy dog // matches
quick brown fox jumps over // matches
the quick brownfox // does not match
foxjumps over the lazy dog // does not match
quick brownfox jumps over // does not match
quick brown foxjumps over // does not match
quick brownfoxjumps over // does not match
有什么办法来实现上述结果与indexOf
或将我需要使用正则表达式?
是什么子,并在这方面,一个字有什么区别? – Y2H
你有没有考虑过使用正则表达式?或者jQuery – evolutionxbox
正则表达式可能是最干净的,但是如果你检查所有的编码,使用空格会有效 –