How to Replace Characters With Underscore in JavaScript

Jagathish Feb 02, 2024
  1. Using replace Method to Replace Characters in JavaScript
  2. Combining split and join Methods in JavaScript
How to Replace Characters With Underscore in JavaScript

This tutorial teaches how to replace specific characters/substrings with other strings in JavaScript. We can use the replace method to replace a substring with an alternate substring.

Using replace Method to Replace Characters in JavaScript

The method replace returns a new string by replacing a substring or a pattern with the replacement string.

For the replace function, we should pass two parameters:

  1. Pattern or substring to be replaced.
  2. String which is to be replaced for the found pattern.

Example:

var str = 'Hi From delftstack  .';
var replacedStr = str.replace(/\s+/g, '_');
console.log('str is : ' + str);
console.log('Replaced String is : ' + replacedStr);

Output:

str is : Hi From delftstack
Replaced String is: Hi_From_delftstack_.

In the above code,

  • Created a string variable str with the value Hi From delftstack .
  • Used the replace method to replace all the spaces in the str variable with the underscore (_) character. For the replace function, the regex /\s+/g is passed as the pattern. In this regex, \s+ will match all the space characters, and the g flag (global flag) is included to match all the pattern occurrences instead of stopping after the first match.
  • The replace method will return a new string that will replace the spaces with an underscore (_). The value in the replaced string will be Hi_From_delftstack_.

Combining split and join Methods in JavaScript

We can achieve the same by combining two methods, split and join.

The split method will create an array by splitting the string into multiple substrings. The string will be split for all the regex-pattern/string matches passed as an argument.

The method join returns a new string created by joining all the array elements. A separator string will be added between each element joining the string (by default, comma , will be added).

Example:

var str = 'Hi From delftstack  .';
var splitArr = str.split(/\s+/g);
var replacedStr = splitArr.join('_');

console.log('str is : ' + str);
console.log('splitArr is : ', splitArr);
console.log('Replaced String is : ' + replacedStr);

Output:

str is : Hi From delftstack
splitArr is : ['Hi', 'From', 'delftstack', '.']
Replaced String is: Hi_From_delftstack_.

In the above code,

  • Created a string variable str with the value Hi From delftstack .
  • Used the split method to with /\s+/g regex as an argument. The split method will divide the string for each match of the space characters. The split method will return ['Hi', 'From', 'delftstack', '.'].
  • Used the join method to join the split strings array with the underscore _ as a separator string and will join all the string elements by adding the underscore between each element. The join method will return Hi_From_delftstack_..

Related Article - JavaScript String

Related Article - JavaScript Methods