Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Replacing spaces with underscores in JavaScript

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given a sentence and the task is to replace the spaces(” “) from the sentence with underscores(“_”). There are some JavaScript methods are used to replace spaces with underscores which are listed below:

JavaScript 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.

JavaScript 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.

Example 1: This example replaces all spaces(‘ ‘) with underscores(“_”) by using replace() method

html




<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>

Output:

Replacing spaces with underscores in JavaScript

Replacing spaces with underscores in JavaScript

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(“_”)

html




<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>

Output:

Replacing spaces with underscores in JavaScript

Replacing spaces with underscores in JavaScript


My Personal Notes arrow_drop_up
Last Updated : 13 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials