简体   繁体   English

正则表达式 - 匹配字符串中的多个无序单词

[英]Regex - match multiple unordered words in a string

I have a list of names and I am looking to filter the list to only return names that contains both the last and first names. 我有一个名单列表,我希望过滤列表只返回包含姓氏和名字的名称。

Let's say I am looking for "Joe Doe" 假设我正在寻找“Joe Doe”

I have the current regex (?:^|\\s)Joe|(?:^|\\s)Doe 我有当前的正则表达式(?:^|\\s)Joe|(?:^|\\s)Doe

It somewhat works but it is returning all the strings that contains either Joe or Doe. 它有点工作,但它返回包含Joe或Doe的所有字符串。 I would like it to match the names that contains both names only, and it could be either "Doe Joe" or "Joe Doe" 我希望它匹配仅包含两个名称的名称,它可以是“Doe Joe”或“Joe Doe”

This lookahead based regex should work: 这个基于前瞻性的正则表达式应该可行:

/(?=.*?\bJoe\b)(?=.*?\bDoe\b).*/i

Testing: 测试:

/(?=.*?\bJoe\b)(?=.*?\bDoe\b).*/.test('Joe Doe'); // true
/(?=.*?\bJoe\b)(?=.*?\bDoe\b).*/.test('Doe Joe'); // true

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

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