0

I have not been able to deploy a working process to use multiple regex patterns to match and replace the following patterns of names. I need to extract the last name, first name, middle initial or fill it with white space, and place commas between them Examples:

In:

  • "HILL,ADAM APRN"
  • "SMITH,JOHN B APRN"
  • "JONES-WILSON,ROBERT APRN"

Out:

  • "HILL,ADAM, ,APRN"
  • "SMITH,JOHN,B,APRN"
  • "JONES-WILSON,ROBERT, ,APRN"

The procedure I used stopped on the first name

Any assistance would greatly be appreciated.

Thank you - Matt

2
  • 2
    Is regexp is really necessary? You can split your strings with split(' ') spaces and then join it back width comma .join(',') and you will get result you need. Commented Jan 23, 2017 at 21:53
  • See this JSFiddle. Commented Jan 23, 2017 at 23:49

2 Answers 2

2

If you really need to use a regular expression, (\w+(?:-\w+)?),(\w+)\s(?:([a-zA-Z]{1})\s)?(\w+) will give you the captures that you need.

However, it would be much easier to use the split function like...

var name = "HILL,ADAM APRN";
var newName = name.split(/[ ,]/).join(',')
Sign up to request clarification or add additional context in comments.

1 Comment

How would I add the white space when the middle initial is not present? i.e. "HILL,ADAM, ,APRN"
0
var str = "HILL,ADAM APRN";
var blocks = str.match(/ /g).length;
var nuStr = (blocks==1 ? (str.replace(/ /, ', ,')) : (str.replace(/ /g, ',')));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.