"The dog chased the cat"找到单词"the" ,你可以使用以下正则表达式: /the/ 。请注意,正则表达式中不需要引号。 JavaScript有多种方法可以使用正则表达式。测试正则表达式的一种方法是使用.test()方法。 .test()方法接受正则表达式,将其应用于字符串(放在括号内),如果模式发现或不存在,则返回true或false 。 让testStr =“freeCodeCamp”;
让testRegex = / Code /;
testRegex.test(testStr);
//返回true
.test()方法在字符串myString上应用正则表达式myRegex 。 .test()来测试正则表达式。
testString: 'assert(code.match(/myRegex.test\(\s*myString\s*\)/), "You should use .test() to test the regex.");'
- text: 您的结果应该返回true 。
testString: 'assert(result === true, "Your result should return true.");'
```