How to remove double underscores using JavaScript

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

eg.

stack__overflow___website

I need to eliminate __and replace it with one _.

+4
source share
2 answers

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

'stack__overflow___website'.replace(/_+/g, '_')
+9
source
var myString = "stack__overflow___website",
    myFormattedString = myString.split('__').join('_');
+2
source

Source: https://habr.com/ru/post/1527690/


All Articles