How can I match a whole word in JavaScript?

前端 未结 4 542
栀梦
栀梦 2020-11-22 02:37

I am trying to search a single whole word through a textbox. Say I search "me", I should find all occurrences of the word "me" in the text, but not "

4条回答
  •  逝去的感伤
    2020-11-22 03:18

    To use a dynamic regular expression see my updated code:

    new RegExp("\\b" + lookup + "\\b").test(textbox.value)
    

    Your specific example is backwards:

    alert((/\b(2)\b/g).test(lookup));
    

    Regexpal

    Regex Object

提交回复
热议问题