javascript 匹配两个字符串的Javascript正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18496715/
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
Javascript regular expression that matches two strings
提问by Enve
I don't understand that much regular expression and I want to create one that matches two strings.
我不明白那么多正则表达式,我想创建一个匹配两个字符串的正则表达式。
I know that for one string the code is .match(/something/)
and if matches on of two strings it is .match(/something|someotherthing/)
我知道对于一个字符串,代码是.match(/something/)
,如果匹配两个字符串,它是.match(/something|someotherthing/)
How to write one that only matches when both strings are found in the text e.g "google microsoft"
如何编写仅在文本中找到两个字符串时才匹配的字符串,例如“google microsoft”
回答by Paul
To do it with a single regular expression, you'll need to account for the fact that they could come in either order. (.*
matches any characters in between the two substrings):
要使用单个正则表达式执行此操作,您需要考虑到它们可以按任一顺序出现的事实。(.*
匹配两个子字符串之间的任何字符):
/google.*microsoft|microsoft.*google/
Note: If you wanted to do more than 2 substrings this would get out of hand. 3 strings could come in 6
different orders, 5 strings could come in 120
orders, and 10 substrings could be ordered 3628800
different ways! That's why using features of the language (Javascript) itself, like in Explosion Pills answer, can be much better than trying to do too much with a regular expression.
注意:如果你想做 2 个以上的子串,这会失控。3 个字符串可以按6
不同的顺序120
排列,5个字符串可以按顺序排列,还有 10 个子串可以按3628800
不同的顺序排列!这就是为什么使用语言 (Javascript) 本身的特性,比如在 Explosion Pills 答案中,比尝试使用正则表达式做太多事情要好得多。
回答by Martin Ender
The other answers are perfectly fine for your specific problem, but as Paulpro noted really get out of hand when you have more than two words.
其他答案非常适合您的特定问题,但正如 Paulpro 指出的那样,当您有两个以上的词时,真的会失控。
The easiest way out is to use multiple checks as Explosion Pills suggests.
最简单的方法是按照 Explosion Pills 的建议使用多次检查。
But for a more scalable regex-only approach you can use lookaheads:
但是对于更具可扩展性的 regex-only 方法,您可以使用lookaheads:
/^(?=.*google)(?=.*microsoft)(?=.*apple).../
The lookahead doesn't actually consume anything, so after the first condition is checked (that .*google
can match), you are back at the beginning of the string and can check the next condition. The pattern only passes if all lookaheads do.
前瞻实际上不消耗任何东西,因此在检查第一个条件(.*google
可以匹配)后,您将返回到字符串的开头并可以检查下一个条件。只有当所有前瞻都通过时,模式才会通过。
Note that if your input may contain line breaks, .*
will not do. You'll have to use [^]*
or [\s\S]*
instead (same goes for the others' answers).
请注意,如果您的输入可能包含换行符,.*
则不会这样做。您必须使用[^]*
或[\s\S]*
代替(其他人的答案也是如此)。
回答by Explosion Pills
You could just use .match
twice
你可以只使用.match
两次
str.match(/google/) && str.match(/microsoft/)
In that case it may be faster to use .indexOf
在这种情况下,使用可能会更快 .indexOf
You can also use a verbose regex
您还可以使用详细的正则表达式
.match(/google.*microsoft|microsoft.*google/)