12

How do I remove double or multiple underscores in a string using JavaScript?

E.g.

stack__overflow___website

I will need to eliminate the __ and replace it with a single _.

0

2 Answers 2

17

You can use replace() with a regex to match consecutive underscores:

'stack__overflow___website'.replace(/_+/g, '_')
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! here's what i got from your code var removeUnderscores = _convertLowerCase.replace(/_+/g, '');
3
var myString = "stack__overflow___website",
    myFormattedString = myString.split('__').join('_');

3 Comments

Interesting approach. I wonder what the performance considerations are here.
this is only one case.
they are supposed to be better. yet downvoted :s stackoverflow.com/questions/441018/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.