Remove an underscore with Regex in JavaScript

You can use .replace to achieve this. Use the following code. It will replace all _ with the second parameter. In our case we don't need a second parameter so all _ will be removed.

<script>
var str = "some_sample_text_here.";
var newStr = str.replace(/_/g , "");
alert ('Text without underscores : ' + newStr);
</script>

Use .replace(/_/g, "") to remove all underscores or use .replace(/_/g, " ") to replace them with a space.

Here is an example to remove them:

var str = "Yeah_so_many_underscores here";
var newStr = str.replace(/_/g, "");
alert(newStr);