admin管理员组文章数量:1431904
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"
I have the current regex (?:^|\s)Joe|(?:^|\s)Doe
It somewhat works but it is returning all the strings that contains either Joe or Doe. I would like it to match the names that contains both names only, and it could be either "Doe Joe" or "Joe Doe"
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"
I have the current regex (?:^|\s)Joe|(?:^|\s)Doe
It somewhat works but it is returning all the strings that contains either Joe or Doe. I would like it to match the names that contains both names only, and it could be either "Doe Joe" or "Joe Doe"
Share Improve this question asked Oct 16, 2013 at 19:47 NewtonNewton 2451 gold badge5 silver badges11 bronze badges 2- is your first name and last name always separated by whitespace? – Rohit Jain Commented Oct 16, 2013 at 19:49
- Not always, there are cases where I have hyphenated names – Newton Commented Oct 16, 2013 at 19:51
1 Answer
Reset to default 8This 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
本文标签: javascriptRegexmatch multiple unordered words in a stringStack Overflow
版权声明:本文标题:javascript - Regex - match multiple unordered words in a string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745591255a2665211.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论