Replacing spaces with underscores in JavaScript
Given a sentence and the task is to replace the spaces(” “) from sentence with underscores(“_”). There are some JavaScript methods are used to replace spaces with underscores which are listed 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.
Return value: It returns a new string where the defines value has been replaced by the new value.
-
split() method: This method is used to split a string into an array of substrings, and returns the new array.
Syntax:
string.split(separator, limit)
Parameters:
- separator: It is optional parameter. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item).
- limit: It is optional parameter. It holds the integer that specifies the number of splits, items beyond the split limit will be excluded from the array.
Return value: It returns a new Array, having the splitted items.
Example 1: This example replaces all spaces(‘ ‘) with underscores(“_”) by using replace() method.
<!DOCTYPE HTML> < html > < head > < title > Replacing spaces with underscores </ 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() { el_down.innerHTML = str.replace(/ /g, "_"); } </ script > </ body > </ html > |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example replaces all spaces(‘ ‘) with underscores(“_”) by using split() method. It first splits the string with spaces(” “) and then join it with underscore(“_”).
<!DOCTYPE HTML> < html > < head > < title > Replacing spaces with underscores </ 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() { el_down.innerHTML = str.split(' ').join('_'); } </ script > </ body > </ html > |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to remove spaces from a string using JavaScript ?
- Create a string with multiple spaces in JavaScript
- How to access an object having spaces in the object's key using JavaScript ?
- PHP | strtr() for replacing substrings
- How to insert spaces/tabs in text using HTML/CSS?
- How to tab space instead of multiple non-breaking spaces (nbsp)?
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | JavaScript Prompt Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.