How to Replace Characters With Underscore in JavaScript
-
Using
replaceMethod to Replace Characters in JavaScript -
Combining
splitandjoinMethods 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:
- Pattern or substring to be replaced.
- 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
strwith the valueHi From delftstack . - Used the
replacemethod to replace all the spaces in thestrvariable with the underscore (_) character. For thereplacefunction, the regex/\s+/gis passed as the pattern. In this regex,\s+will match all the space characters, and thegflag (global flag) is included to match all the pattern occurrences instead of stopping after the first match. - The
replacemethod will return a new string that will replace the spaces with an underscore (_). The value in the replaced string will beHi_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
strwith the valueHi From delftstack . - Used the
splitmethod to with/\s+/gregex as an argument. Thesplitmethod will divide the string for each match of the space characters. Thesplitmethod will return['Hi', 'From', 'delftstack', '.']. - Used the
joinmethod 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. Thejoinmethod will returnHi_From_delftstack_..
Related Article - JavaScript String
- How to Get Last Character of a String in JavaScript
- How to Convert a String Into a Date in JavaScript
- How to Get First Character From a String in JavaScript
- How to Convert Array to String in JavaScript
- How to Check String Equality in JavaScript
- How to Filter String in JavaScript