cyberRobot Posted May 6, 2019 Share Posted May 6, 2019 I'm looking to extract names from a string that is typically formatted like the following examples: Paul Jones and Mike Smith Emily Salazar, Marcia Dunn, and John Miller Mike Smith The following code seems to be working: var inputValues = inputString.split(/, (?:and )?|(?: and )/); Does anyone have any ideas for streamlining the regex portion? I'm especially interested if there's a way to write it without the straight bar (|). Quote Link to comment https://forums.phpfreaks.com/topic/308674-extracting-names-with-regex/ Share on other sites More sharing options...
requinix Posted May 7, 2019 Share Posted May 7, 2019 I might replace the " and " with ", ", then split. inputString.replace(/ and /g, ", ").split(/,[\s,]*/) 1 Quote Link to comment https://forums.phpfreaks.com/topic/308674-extracting-names-with-regex/#findComment-1566483 Share on other sites More sharing options...
cyberRobot Posted May 7, 2019 Author Share Posted May 7, 2019 That's much cleaner. Thanks for the suggestion! Quote Link to comment https://forums.phpfreaks.com/topic/308674-extracting-names-with-regex/#findComment-1566501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.