简体   繁体   English

使用正则表达式匹配一个或多个单词的一部分

[英]Using regex to match part of a word or words

I'm new to regex and having difficulty with some basic stuff. 我是正则表达式的新手,并且遇到了一些基本的问题。

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. 澄清:我不只是试图通过我给出的5个例子来测试,我试图想出一些正则表达式,其中任何一个测试都会通过。 So, for example: 所以,例如:

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". 基本上我需要编写一个函数,它接受任何searchTerm(它不包括在这里,但在其他地方我要求输入至少3个字符)并且可以检查在给定的字符串中是否按顺序找到这3个字母“名字姓氏”

"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. 如果字符串有任何匹配,则返回true (better to use this inside a condition, because you don't need to find all the matches). (最好在条件中使用它,因为你不需要找到所有匹配项)。

  • \\b - means word boundary. \\b - 表示单词边界。
  • () - capturing group. () - 捕获组。
  • ? - quantifier "one or none". - 量词“一个或没有”。
  • | - logical "or". - 逻辑“或”。

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 | 要匹配字符串中的值,您需要使用布尔OR与|匹配 - using the i flag will cause a case insensitive match so you don't need to call toLowerCase() - - 使用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"). 这将导致matches数组有5个元素(除了“paul”之外的每个名称)。 Worth noting that this would match additional names as well, such as "rob johnson" and "rob john" which may not be desired. 值得注意的是,这也会与其他名字相匹配,例如“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!');
}

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)

/^\\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 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. 如果您不想这样,请从模式的开头和结尾删除\\s* Also, the space and asterisk between name and surname allows 0 or more spaces between those two. 此外,名称和姓氏之间的空格和星号允许这两者之间有0个或更多空格。 If you don't want it to contain any space, just get rid of that space and asterisk. 如果您不希望它包含任何空间,只需删除该空格和星号。

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标志使搜索案例不敏感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM