我想要一个代码来检查整个和精确的单词匹配:)所以在我的逻辑中look不匹配looked。let speech = "you ever looked at someone";let a = "look";if(speech.includes(a)){ console.log("Whole Word Matches")} else { console.log("No Match!")}如何修改上面的代码以检查整个单词匹配
2 回答
HUWWW
TA贡献1874条经验 获得超12个赞
if(new RegExp("\\b"+a+"\b").test(speech)){
console.log("Whole Word Matches")
} else {
console.log("No Match!")
}
素胚勾勒不出你
TA贡献1827条经验 获得超9个赞
您可以使用正则表达式(\bregExp\b)来查找单词的完全匹配项。
The \b metacharacter is used to find a match at the beginning or end of a word.
let speech = "you ever looked at someone";
let a = "look";
if(new RegExp("\\b"+a+"\\b").test(speech)){
console.log("Whole Word Matches")
} else {
console.log("No Match!")
}
添加回答
举报
0/150
提交
取消