I have a string that looks like this: "Doe, John, A" (lastname, firstname, middle initial).
I'm trying to write a regular expression that converts the string into "Doe*John*A".
However, I have to take into account all spaces for this string so "Doe , John , A" would still convert into "Doe*John*A".
ALSO, the string "Doe John A" should convert into "Doe*John*A".
I started writing this, but I think I'm stuck on the spaces & the possibility of the user not supplying the commas.
Here's what I have:
var myString = "John, Doe, A";
var myOtherString = "John Doe A";
var myFunction = function (aString) {
aString = aString.replace(", ", "*");
aString = aString.replace(", ", "*");
return aString;
};
These should both return "Doe*John*A"
.
I think I'm repeating myself too much in this function. I'm also not taking into account the possibility that no commas will be provided.
Is there a better way to do this?