Javascript 使用正则表达式匹配动态字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2145073/
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 22:56:51  来源:igfitidea点击:

Match dynamic string using regex

javascriptregex

提问by Fares Farhan

I'm trying to detect an occurrence of a string within string. But the code below always returns "null". Obviously something went wrong, but since I'm a newbie, I can't spot it. I'm expecting that the code returns "true" instead of "null"

我试图检测字符串中字符串的出现。但是下面的代码总是返回“null”。显然出了点问题,但由于我是新手,我无法发现它。我期待代码返回“true”而不是“null”

var searchStr = 'width'; 
var strRegExPattern = '/'+searchStr+'\b/'; 
"32:width: 900px;".match(new RegExp(strRegExPattern,'g'));

回答by YOU

Please don't put '/' when you pass string in RegExp option

在 RegExp 选项中传递字符串时请不要放“/”

Following would be fine

关注就好了

var strRegExPattern = '\b'+searchStr+'\b'; 
"32:width: 900px;".match(new RegExp(strRegExPattern,'g'));

回答by Alan Moore

You're mixing up the two ways of creating regexes in JavaScript. If you use a regex literal, /is the regex delimiter, the gmodifier immediately follows the closing delimiter, and \bis the escape sequence for a word boundary:

您正在混淆在 JavaScript 中创建正则表达式的两种方法。如果您使用正则表达式文字,/是正则表达式分隔符,g修饰符紧跟在结束分隔符之后,并且\b是单词边界的转义序列:

var regex = /width\b/g;

If you create it in the form of a string literal for the RegExp constructor, you leave off the regex delimiters, you pass modifiers in the form of a second string argument, and you have to double the backslashes in regex escape sequences:

如果您以字符串文字的形式为 RegExp 构造函数创建它,您将省略正则表达式分隔符,以第二个字符串参数的形式传递修饰符,并且您必须将正则表达式转义序列中的反斜杠加倍:

var regex = new RegExp('width\b', 'g');

The way you're doing it, the \bis being converted to a backspace character before it reaches the regex compiler; you have to escape the backslash to get it past JavaScript's string-literal escape-sequence processing. Or use a regex literal.

您这样做的方式是在\b到达正则表达式编译器之前将其转换为退格字符;你必须转义反斜杠才能让它通过 JavaScript 的字符串文字转义序列处理。或者使用正则表达式文字。

回答by adamJLev

The right tool for this job is not regex, but String.indexOf:

这项工作的正确工具不是正则表达式,而是 String.indexOf:

var str = '32:width: 900px;',
    search = 'width',
    isInString = !(str.indexOf(search) == -1); 

// isInString will be a boolean. true in this case

Documentation: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/indexOf

文档:https: //developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/indexOf

回答by Anonymous

Notice that '\\b' is a single slash in a string followed by the letter 'b', '\b' is the escape code \b, which doesn't exist, and collapses to 'b'.

请注意,'\\b' 是字符串中的单斜杠,后跟字母 'b','\b' 是\b不存在的转义码,并折叠为 'b'。

Also consider escaping metacharacters in the string if you intend them to only match their literal values.

如果您希望它们只匹配它们的文字值,还可以考虑转义字符串中的元字符。

var string = 'width';
var quotemeta_string = string.replace(/[^$\[\]+*?.(){}\|]/g, '\'); // escape meta chars
var pattern = quotemeta_string + '\b';
var re = new RegExp(pattern);
var bool_match = re.test(input); // just test whether matches
var list_matches = input.match(re); // get captured results