3

Can someone help me with replacing consecutive spaces with a hyphen? For example, I need:

123      321 

to become

123-321

Thanks in advance!

2 Answers 2

8

var result = "123      321".replace(/ +/g, "-");
console.log(result);

/ +/g = at least 1 space, look globally (in the whole string)

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

Comments

1

The regular expression \s+ will match any number of consecutive spaces (including tabs and other whitespace characters). Use that as a global pattern for string.replace().

Example from a Javascript console:

> "a     b".replace(/\s+/g, "-")
"a-b"

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.