Reputation: 3399
I've found many solutions in JavaScript to remove all spaces in a string with \s
. But I can't found a solution for my specific problem. I want to transform "2* 2 +3* 5" in "2*2+3*5" (no more space after '*'), I've tried with
mot = mot.replace(/*\s/g, '*');
But it doesn't work, does anyone have the answer?
Upvotes: 0
Views: 2166
Reputation: 6809
mot = mot.replace(/(\*)\s/g, "$1");
Note that you have to use \
before a *
because *
is a reserved character. With this, you can also add more characters with no whitespace allowed after, like *
and +
here:
mot = mot.replace(/([*+])\s/g, "$1");
Now that the *
is in a []
you don't need to esacpe it. And lastly, to remove multiple whitespaces at once, use
mot = mot.replace(/([*+])\s+/g, "$1");
Upvotes: 0
Reputation: 6322
You need to escape the *
character.
Try this: "2* 2 +3* 5".replace(/\*\s/g,'*')
Upvotes: 1