Javascript javascript中的全字匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2232934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:22:22  来源:igfitidea点击:

whole word match in javascript

javascriptregex

提问by vondip

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。我正在尝试通过文本框搜索一个完整的单词。假设我搜索: 'me' ,我应该找到文本中的所有 'me',但每次都找不到 'memmm' 。

I am using javascript's search('my regex expression') to perform the current search (with no success).

我正在使用 javascript 的 search('my regex expression') 来执行当前的搜索(没有成功)。

Thank you!

谢谢!

Edit: After several proposals to use the \b switches [which don't seem to work] I am posting a revised explanation of my problem:

编辑:在使用 \b 开关的几个建议 [似乎不起作用] 后,我发布了对我的问题的修订解释:

amm, from some reason this doesn't seemt to do the tricks. Assume the following javascript search text:

嗯,出于某种原因,这似乎不起作用。假设以下 javascript 搜索文本:

<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>

Moving lines is essential

动线很重要

回答by ChaosPandion

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

正则表达式对象

回答by Gumbo

Use the word boundary assertion \b:

使用词边界断言\b

/\bme\b/

回答by Tobi

You may use the following code:

您可以使用以下代码:

var stringTosearch ="test ,string, test"; //true
var stringTosearch ="test string test"; //true
var stringTosearch ="test stringtest"; //false
var stringTosearch ="teststring test"; //false

if (new RegExp("\b"+"string"+"\b").test(stringTosearch)) {
  console.log('string found');
  return true;
} else {
  return false;
}

回答by Brian Campbell

<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>

It's a bit hard to tell what you're trying to do here. What is the ttttvariable supposed to do?

很难说你在这里想做什么。什么是tttt应该做的变量?

Which string are you trying to search in? Are you trying to look for 2within the string lookup? Then you would want:

您要搜索哪个字符串?你想2在字符串中寻找lookup吗?那么你会想要:

/\b2\b/.test(lookup)

The following, from your regular expression, constructs a regular expression that consists of a word boundary, followed by the string "lookup"(not the value contained in the variable lookup), followed by a word boundary. It then tries to match this regular expression against the string "2", obtained by converting the number 2to a string:

下面从您的正则表达式构造一个正则表达式,该正则表达式由单词边界、后跟字符串"lookup"(不是变量中包含的值lookup)和单词边界组成。然后尝试将此正则表达式与"2"通过将数字转换为字符串获得的 string 进行匹配2

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

For instance, the following returns true:

例如,以下返回true

(/\b(lookup)\b/g).test("something to lookup somewhere")