regex to match on a person's name as enter
I am trying to return a match if none of the following is found anywhere in the person's name when entered (case insensitive):
Dr Dr. Mr Mr. Mrs Mrs. Ms Ms. Miss and + & PHD Sr Sr. Jr Jr.
It also needs to look for spaces on either side of the above so as not non match "Drake" or "Missy", etc..
I'm not very good a regex and trying this in regexbuddy is also 开发者_运维百科defficult.. Anyone here able to help out? What I have so far works ok but on for the front of a string but someone could enter dennis dr dennis and be fine...
^\s*(?!(?:(\s*)Dr|Mr|Mrs|Miss|Ms)\b).*?$
You can use following regular expression:
/(?:^|\W)(?:(?:Dr|Mr|Mrs|Ms|Sr|Jr)\.?|Miss|Phd|\+|&)(?:\W|$)/i
EDIT: Alternative regex based on negative lookahead:
/(?!.*?(?:^|\W)(?:(?:Dr|Mr|Mrs|Ms|Sr|Jr)\.?|Miss|Phd|\+|&)(?:\W|$))^.*$/i
What about
^\s*(?!(?:Dr|Mr|Mrs|Miss|Ms)\.?\b)(?!.*\b(?:\+|&|PHD|Sr|Jr)(?:(?:\.\B)|\b)).*?$
See it here on Regexr
I added a second part to your regex
(?!.*\b(?:\+|&|PHD|Sr|Jr)(?:(?:\.\B)|\b))
This looks for the things somewhere in the string with a word boundary before and after except when it ends with a dot then followed by a non word boundary.
精彩评论