整个单词匹配在JavaScript中 [英] whole word match in javascript
问题描述
我在网页上使用javascript。我试图通过文本框搜索一个完整的单词。说我搜索:'我',我应该在文本中找到所有'我',但不会发现每个说'memmm'。
I am using javascript in my webpage. I am trying to search a single whole word through textbox. Say I search: 'me' , I should find all the 'me' in the text, but not find 'memmm' per say.
我正在使用javascript的搜索( '我的正则表达式')执行当前搜索(没有成功)。
I am using javascript's search('my regex expression') to perform the current search (with no success).
谢谢!
编辑:在几个提议使用\b开关[似乎不起作用]后,我发布了我的问题的修订说明:
After several proposals to use the \b switches [which don't seem to work] I am posting a revised explanation of my problem:
amm,from some some原因这似乎没有做的伎俩。假设以下javascript搜索文本:
amm, from some reason this doesn't seemt to do the tricks. Assume the following javascript search text:
<script type='text/javascript'>
var lookup = '\n\n\n\n\n\n2 PC Games \n\n\n\n';
lookup = lookup.trim() ;
alert(lookup );
var tttt = 'tttt';
alert((/\b(lookup)\b/g).test(2));
</script>
移动线是必不可少的
推荐答案
要使用动态正则表达式,请参阅我的更新代码:
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));
这篇关于整个单词匹配在JavaScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!