简体   繁体   中英

trying to match first name titles.. or non-match as the case may be

I am tryign to return a no match if a person enters a title in the input box I have setup..

What I have so far is:

^\s*(?!Dr|Mr|Mrs|Miss|Ms).*?$

This works fine except it won't match "Drake", "Missy" and it even matches "Dr.Dennis" or " Dr Dennis".. I know it would be easier to do a reverse and match on the person's title but I'd have to rewrite our entire validaton rules since all the others return matches and error's out on non matches.

Anyone have an idea how I can do this?

You can add a word boundary to your negative look ahead like this

^\s*(?!(?:Dr|Mr|Mrs|Miss|Ms)\b).*?$

See it here on Regexr

Hope I understood you correctly. This word boundary \\b ensures that there is a non word character after your title, so that it will not ignore stuff like Drake.

The (?:) is a non capturing group, meaning the stuff from your list is not put in a variable as result like a normal group () would do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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