Jquery 正则表达式单词或短语的精确匹配

Jquery 正则表达式单词或短语的精确匹配,jquery,regex,match,Jquery,Regex,Match,只有当给定的单词或短语出现时,我才需要匹配 My name is Kristina :- match Here is Kristinatt :- not a match Kristina is coming :- match Akristina is not a name :- not a match Kristina is a good name :- match 这是我的jQuery代码 var needle = "Kristina"; var hay

只有当给定的单词或短语出现时,我才需要匹配

My name is Kristina      :- match
Here is Kristinatt       :- not a match
Kristina is coming       :- match
Akristina is not a name  :- not a match
Kristina is a good name  :- match
这是我的jQuery代码

var needle = "Kristina";
var haystack = "Here is Kristinatt";
var search_regexp = new RegExp('\\b' + needle + '\\b', "gi");
if(haystack.match(search_regexp) != null){
    alert("match found");
}else{
    alert("no match found");
}

理想情况下,我应该找不到匹配的针和干草堆的组合。关于我可能做错了什么有什么想法吗?

您的代码正在运行,请检查:

另一个解决方案:

var needle = "Kristina",
    haystack = "Here is Kristinatt",
    check = haystack.split(/\s+|\./).includes(needle);
if(check){
    alert("match found");
}else{
    alert("no match found");
}
这是一把小提琴:

如果这不是你的意思,请澄清你在寻找什么。

我确实得到“未找到匹配项”