0

Let's say I have a string 'Hello_World'.

How do I remove '_' from the string for it to be 'Hello World'?

I have seen various trim examples like the following from w3schools:

function myTrim(x) {
    return x.replace(/^\s+|\s+$/gm,'');
}

to remove whitespaces from a string. I dont know how to remove an underscore using the same methodology.

vapurrmaid
  • 2,287
  • 2
  • 14
  • 30
emilkitua
  • 95
  • 2
  • 3
  • 12

4 Answers4

2

You can try by replacing with the following regex s/_//g

2

A non-regex way can be:

var string = 'Hello_World';
string = string.split('_').join('');
console.log(string)
Palash Karia
  • 670
  • 4
  • 10
1

You can simply use replace(/_/g,' ') to remove all underscore globally from the string:

var res = "Hello_World".replace(/_/g,' ');
console.log(res);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
1

for example you can use this:

var mystring = "this_is_a_test"
console.log(mystring.replace(/_/g , ""));
Wolkowsky
  • 68
  • 1
  • 6