javascript 使用正则表达式匹配一个或多个单词的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12993273/
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
Using regex to match part of a word or words
提问by Jeremy Ricketts
I'm new to regex and having difficulty with some basic stuff.
我是 regex 的新手,对一些基本的东西有困难。
var name = "robert johnson";
var searchTerm = "robert johnson";
if (searchTerm.match(name)) {
console.log("MATCH");
}
I'd like to try and find something that matches any of the following:
我想尝试找到符合以下任何一项的内容:
rob, robert, john, johnson, robertjohnson
To make the regex simpler, I've already added a .toLowerCase() to both the "name" and the "searchTerm" vars.
为了使正则表达式更简单,我已经在“name”和“searchTerm”变量中添加了一个 .toLowerCase() 。
What regex needs to be added to searchTerm.match(name) to make this work?
需要将什么正则表达式添加到 searchTerm.match(name) 才能使其工作?
Clarification:I'm not just trying to get a test to pass with the 5 examples I gave, I'm trying to come up with some regex where any of those tests will pass. So, for example:
澄清:我不只是想通过我给出的 5 个示例来通过测试,我还试图想出一些正则表达式,其中任何一个测试都会通过。因此,例如:
searchTerm.match(name)
...needs to change to something like:
...需要更改为:
searchTerm.match("someRegexVoodooHere"+name+"someMoreRegexVoodooHere")
So, if I edit
所以,如果我编辑
var searchTerm = "robert johnson";
...to be
...成为
var searchTerm = "rob";
...the same function searchTerm.match directive would work.
...相同的函数 searchTerm.match 指令将起作用。
Again, I'm new to regex so I hope I'm asking this clearly. Basically I need to write a function that takes any searchTerm (it's not included here, but elsewhere I'm requiring that at least 3 characters be entered) and can check to see if those 3 letters are found, in sequence, in a given string of "firstname lastname".
同样,我是 regex 的新手,所以我希望我能清楚地问这个问题。基本上我需要编写一个接受任何 searchTerm 的函数(它不包括在这里,但在其他地方我要求至少输入 3 个字符)并且可以检查是否在给定的字符串中按顺序找到了这 3 个字母的“名字姓氏”。
回答by
"robert johnson".match(/\b(john(son)?|rob(ert(johnson)?)?)\b/)
Will give you all possible matches (there are more then one, if you need to find whether the input string contained any of the words.
将为您提供所有可能的匹配项(如果您需要查找输入字符串是否包含任何单词,则有多个匹配项。
/\b(john(son)?|rob(ert(johnson)?)?)\b/.test("robert johnson")
will return true
if the string has any matches. (better to use this inside a condition, because you don't need to find all the matches).
true
如果字符串有任何匹配项,将返回。(最好在条件中使用它,因为您不需要找到所有匹配项)。
\b
- means word boundary.()
- capturing group.?
- quantifier "one or none".|
- logical "or".
\b
- 表示词边界。()
- 捕获组。?
- 量词“一或无”。|
- 逻辑“或”。
回答by doublesharp
Regular expressions look for patterns to make a match. The answer to your question somewhat depends on what you are hoping to accomplish - That is, do you actually want matched groups or just to test for the existence of a pattern to execute other code.
正则表达式寻找模式来进行匹配。您的问题的答案在某种程度上取决于您希望完成的任务 - 也就是说,您是真的想要匹配组还是只是为了测试是否存在模式来执行其他代码。
To match the values in your string, you would need to use boolean OR matching with a |
- using the i
flag will cause a case insensitive match so you don't need to call toLowerCase()
-
要匹配字符串中的值,您需要使用布尔值 OR 与 a 匹配|
- 使用该i
标志将导致不区分大小写的匹配,因此您无需调用toLowerCase()
-
var regex = /(rob|robert|john|johnson|robertjohnson)/i;
regex.match(name);
If you want a more complex regex to match on all of these variations -
如果你想要一个更复杂的正则表达式来匹配所有这些变化 -
var names = "rob, robert, john, johnson, robertjohnson, paul";
var regex = /\b((rob(ert)?)?\s?(john(son)?)?)\b/i;
var matches = regex.match(names);
This will result in the matches
array having 5 elements (each of the names except "paul"). Worth noting that this would match additional names as well, such as "rob johnson" and "rob john" which may not be desired.
这将导致matches
数组有 5 个元素(除了“paul”之外的每个名称)。值得注意的是,这也会匹配其他名称,例如可能不需要的“rob johnson”和“rob john”。
You can also just test if your string contains any of those terms using test()
-
您还可以使用test()
-测试您的字符串是否包含任何这些术语
var name = "rob johnson";
var regex = /\b((rob(ert)?)?\s?(john(son)?)?)\b/i;
if (regex.test(name)){
alert('matches!');
}
回答by charlietfl
You could create an array of the test terms and loop over that array. This method means less complicated regex to build in a dynamic environment
您可以创建一个测试项数组并遍历该数组。这种方法意味着在动态环境中构建更简单的正则表达式
var name = "robert johnson";
var searchTerm = "robert johnson";
var tests = ['rob', 'robert', 'john', 'johnson', 'robertjohnson'];
var isMatch = false;
for (i = 0; i < tests.length; i++) {
if (searchTerm.test(tests[i])) {
isMatch = true;
}
}
alert(isMatch)
回答by inhan
/^\s*(rob(ert)?|john(son)?|robert *johnson)\s*$/i.test(str)
/^\s*(rob(ert)?|john(son)?|robert *johnson)\s*$/i.test(str)
will return true if str
matches either:
如果str
匹配,将返回 true :
- rob
- robert
- john
- johnson
- robertjohnson
- robert johnson
- robert johnson (spaces between these 2 does not matter)
- 抢
- 罗伯特
- 约翰
- 约翰逊
- 罗伯特约翰逊
- 罗伯特·约翰逊
- 罗伯特约翰逊(这两个之间的空格无关紧要)
and it just doesn't care if there are preceding or following empty characters. If you don't want that, delete \s*
from the beginning and the end of the pattern. Also, the space and asterisk between name and surname allows 0 or more spaces between those two. If you don't want it to contain any space, just get rid of that space and asterisk.
它并不关心前面或后面是否有空字符。如果您不想那样,请\s*
从模式的开头和结尾删除。此外,姓名和姓氏之间的空格和星号允许这两者之间有 0 个或多个空格。如果您不希望它包含任何空格,只需去掉那个空格和星号。
The caret ^
indicates beginning of string and the dollar sign $
indicates end of string. Finally, the i
flag at the end makes it search case insensitively.
插入符号^
表示字符串的开头,美元符号$
表示字符串的结尾。最后,i
末尾的标志使其不区分大小写。