Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove double underscore using JavaScript [duplicate]

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 _.

like image 685
nhoyti Avatar asked Feb 19 '14 10:02

nhoyti


2 Answers

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

'stack__overflow___website'.replace(/_+/g, '_')
like image 83
techfoobar Avatar answered Oct 24 '22 06:10

techfoobar


var myString = "stack__overflow___website",
    myFormattedString = myString.split('__').join('_');
like image 45
Tim Vermaelen Avatar answered Oct 24 '22 07:10

Tim Vermaelen