javascript - Regex - match multiple unordered words in a string - Stack Overflow

I have a list of names and I am looking to filter the list to only return names that contains both the

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
Add a ment  | 

1 Answer 1

Reset to default 8

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745591256a4634845.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信