0

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?

2 Answers 2

1

You need to escape the * character.

Try this: "2* 2 +3* 5".replace(/\*\s/g,'*')

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

Comments

0
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");

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.