I’m trying to figure out a regular expression in JS that matches between two characters but there can be like two different variations so it needs to know that. I need to match between comma and (, OR comma and letter followed by a period. (“T.”)
Here’s the data I have:
Doe, John (SUP)
Doe, John T. (SUP)
Doe, John Smith (SUP)
Doe, John Smith T. (SUP)
Doe, John-Smith (SUP)
Doe, John-Smith T. (SUP)
I need to match just the first name from that. So it would be like this:
John
John
John Smith
John Smith
John-Smith
John-Smith
Here’s the code I have so far:
var nameLinkAdd = nameLink.match(/\,(.*?)\(/g);
Any suggestions? Thanks!
This will work if you don’t necessarily have the
(SUP)after. Ie it would work onThe
(?!.)makes sure you don’t capture anything followed by a peroid..If you want to avoid lookarounds (could be a speed issue, although with such short strings I don’t think so), try:
However this assumes that all names have more than one letter, and start with one capital followed by lowercase (which seems reasonable, are there any names with internal uppercase letters or names consisting of just one letter?)