Replace special characters in a string with underscore (_) in JavaScript
JavaScript replace() method is used to replace all special characters from a string with _ (underscore) which is described below:
- replace() method: This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax:
string.replace(searchVal, newvalue)
- Parameters:
- searchVal: It is required parameter. It specifies the value, or regular expression, that is going to replace by the new value.
- newvalue: It is required parameter. It specifies the value to replace the search value with.
Example 1: This example replaces all special characters with _ (underscore) using replace() method.
html
<!DOCTYPE HTML> < html > < head > < title > Replace special characters in a string with _ (underscore) </ title > </ head > < body style = "text-align:center;"> < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 16px; font-weight: bold;"> </ p > < button onclick = "gfg_Run()"> Click here </ button > < p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = "This, is# GeeksForGeeks!"; el_up.innerHTML = str; function gfg_Run() { el_down.innerHTML = str.replace(/[&\/\\#, +()$~%.'":*?<>{}]/g, '_'); } </ script > </ body > </ html > |
Output:
- Before clicking on the button:
- After clicking on the button:
Example 2: This example replace a unique special character with _ (underscore). This example goes to each character and checks if it is a special character that we are looking for, then it will replace the character. In this example the unique character is $(dollar sign).
html
<!DOCTYPE HTML> < html > < head > < title > Replace special characters with _ (underscore) </ title > </ head > < body style = "text-align:center;"> < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 16px; font-weight: bold;"> </ p > < button onclick = "gfg_Run()"> Click here </ button > < p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = "A$computer$science$portal$for$Geeks"; el_up.innerHTML = str; function gfg_Run() { var newStr = ""; for(var i=0; i< str.length ; i++) { if (str[i] == '$') { newStr += '_'; } else { newStr += str[i]; } } el_down.innerHTML = newStr ; } </script> </ body > </ html > |
Output:
- Before clicking on the button:
- After clicking on the button:
Example 3: This example replace a unique special character with _ (underscore). This example spread function is used to form array from string and form string with the help of reduce which excludes all special character and add underscore in there places. In this example the unique character are `&\/#, +()$~%.'”:*?<>{}`.
Example:
HTML
<!DOCTYPE HTML> < html > < head > < title > Replace special characters in a string with _ (underscore) </ title > </ head > < body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 16px; font-weight: bold;" > </ p > < button onclick = "gfg_Run()" > Click here </ button > < p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;" > </ p > < script > var check = chr => `&\/#, +()$~%.'":*?<>{}`.includes(chr); var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = "This, is# GeeksForGeeks!"; el_up.innerHTML = str; var underscore_str = [...str].reduce((s, c) => check(c) ? s+'_' : s + c, '') function gfg_Run() { el_down.innerHTML = underscore_str; } </ script > </ body > </ html > |
Output:
- Before clicking button:
reduce() function
- After clicking Button:
reduce() function