猿问

检查整个和精确的单词匹配

我想要一个代码来检查整个和精确的单词匹配:)


所以在我的逻辑中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!")

}


如何修改上面的代码以检查整个单词匹配


慕斯王
浏览 144回答 2
2回答

HUWWW

if(new RegExp("\\b"+a+"\b").test(speech)){ console.log("Whole Word Matches")} else {  console.log("No Match!")}

素胚勾勒不出你

您可以使用正则表达式(\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!")}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答