How to globally replace a forward slash in a JavaScript string ?

Method 1: Using replace() method with a regular expression: The replace() method is used to replace the given pattern with another string. The pattern string can be a string or a regular expression. This function will return a new string with the replaced string.

A regular expression is used to replace all the forward slashes. As the forward slash (/) is special character in regular expressions, it has to be escaped with a backward slash (\). Also, to replace all the forward slashes on the string, the global modifier (g) is used. It will replace all the forward slashes in the given string.

Syntax:



originalString.replace(/\//g, replacementString)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>
        How to globally replace a forward
        slash in a JavaScript string?
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
      
    <b>
        How to globally replace a forward slash
        in a JavaScript string?
    </b>
      
    <p>
        The original string is: 
        string / with some // slashes /
    </p>
      
    <p>
        The string after replacing the
        forward slashes is:
        <span class="output"></span>
    </p>
      
    <button onclick="replaceSlashes()">
        Replace Slashes
    </button>
  
    <script type="text/javascript">
        function replaceSlashes() {
  
            let origString = 'string / with some // slashes /';
            let replacementString = '*';
            let replacedString = 
                origString.replace(/\//g, replacementString);
              
            document.querySelector('.output').textContent = 
                        replacedString;
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
    using-replace-before
  • After clicking the button:
    using-replace-after

Method 2: Splitting in place of the forward-slash and joining it back with required string: The split() method is used to separate a string into an array of strings based on the separator. The string is first separated on the basis of the forward slash as the separator. It will give an array of strings separated at the point where the forward slash was present.
The join() method is used to concatenate an array of strings with a specified separator. The required character to be used instead of the forward character is passed as a parameter here. This will replace all the forward slashes in the given string.

Syntax:

origString.split('/').join(replacementString)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        How to globally replace a forward
        slash in a JavaScript string?
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
      
    <b>
        How to globally replace a forward
        slash in a JavaScript string?
    </b>
      
    <p>
        The original string is: 
        string / with some // slashes /
    </p>
      
    <p>
        The string after replacing the
        forward slashes is:
        <span class="output"></span>
    </p>
      
    <button onclick="replaceSlashes()">
        Replace Slashes
    </button>
  
    <script type="text/javascript">
        function replaceSlashes() {
  
            let origString = 'string / with some // slashes /';
            let replacementString = '*';
            let replacedString = 
                 origString.split('/').join(replacementString);
              
            document.querySelector('.output').textContent = 
                        replacedString;
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
    using-splitjoin-before
  • After clicking the button:
    using-splitjoin-after


My Personal Notes arrow_drop_up

Technical Writer Lets have an update

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.