13

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?

4 Answers 4

17

Yes, there is. Use the replace function with a regex instead. That has a few advantages. Firstly, you don't have to call it twice anymore. Secondly it's really easy to account for an arbitrary amount of spaces and an optional comma:

aString = aString.replace(/[ ]*,[ ]*|[ ]+/g, '*');

Note that the square brackets around the spaces are optional, but I find they make the space characters more easily readable. If you want to allow/remove any kind of whitespace there (tabs and line breaks, too), use \s instead:

aString = aString.replace(/\s*,\s*|\s+,/g, '*');

Note that in both cases we cannot simply make the comma optional, because that would allow zero-width matches, which would introduce a * at every single position in the string. (Thanks to CruorVult for pointing this out)

Sign up to request clarification or add additional context in comments.

4 Comments

"Doe , John , A".replace(/\s*,?\s*/g, '*');//"*D*o*e**J*o*h*n**A*"
What if I wanted to replace the *s with dashes? Would I just change this to be aString = aString.replace(/[ ]*,[ ]*|[ ]+/g, '-'); ? --thanks for the helpful answer, btw.
@1__ yup, the second parameter is the replacement string
@CruorVult will you please solve my question? i know you have solved such type of issue. link
8

If you want to replace all non-word characters try this:

str.replace(/\W+/g, '*');

Comments

3

String.replace only replaces the first occurence. To replace them all, add the "g" flag for "global". You can also use character groups and the +operator (one or more) to match chains of characters:

aString.replace("[,\s]+", "*", "g");

This will replace all chains of commas and whitespaces with a *.

1 Comment

again, this is not how regexes are written and how modifiers are applied in javascript
1

Try this out to remove all spaces and commas, then replace with *.

Myname= myname.replace(/[,\s]/,"*")

Editted as removing 'at least two items' from the pattern. But to have at least on item.

Myname= myname.replace(/([,\s]{1,})/,"*")

Reference: on Rublar. However you are better off with regexpal as per m.buettner :)

5 Comments

You should have made it "at least one item". Now you will get as many * as you had spaces and commas before
Yup, that's right, but that's not the pattern in your answer (you should use regexpal though, since it's a Javascript question)
@m.buettner thank you for regexpal. I usually depend on multiple chopping in rublar then adjust it for c#,javascript etc.. Now this makes things convenient.
there is also RegexHero for .NET and RegexPlanet for a few other ones (it's mostly, the go-to tester for Java, though, I think)

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.