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?

Jeff
  • 1,800
  • 8
  • 30
  • 54

4 Answers4

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)

Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • `"Doe , John , A".replace(/\s*,?\s*/g, '*');//"*D*o*e**J*o*h*n**A*"` – CruorVult Dec 19 '12 at 16:01
  • 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. – Jeff Dec 19 '12 at 18:09
  • @1__ yup, the second parameter is the replacement string – Martin Ender Dec 19 '12 at 22:22
  • @CruorVult will you please solve my question? i know you have solved such type of issue. [link](http://stackoverflow.com/questions/27010351/unbalanced-calls-to-begin-end-appearance-transitions-for-qlremotepreviewcontent) – RamGrg Nov 20 '14 at 07:12
8

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

str.replace(/\W+/g, '*');
CruorVult
  • 823
  • 1
  • 9
  • 17
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 *.

Philipp
  • 67,764
  • 9
  • 118
  • 153
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 :)

bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • You should have made it "at least one item". Now you will get as many `*` as you had spaces and commas before – Martin Ender Dec 19 '12 at 22:23
  • Yup, that's right, but that's not the pattern in your answer (you should use [regexpal](http://regexpal.com) though, since it's a Javascript question) – Martin Ender Dec 19 '12 at 23:20
  • @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. – bonCodigo Dec 19 '12 at 23:40
  • there is also [RegexHero](http://regexhero.net/) for .NET and [RegexPlanet](http://www.regexplanet.com/) for a few other ones (it's mostly, the go-to tester for Java, though, I think) – Martin Ender Dec 20 '12 at 00:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21420/discussion-between-boncodigo-and-m-buettner) – bonCodigo Dec 20 '12 at 00:25