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

16 Oct, 2019
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:
<!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>
Output:
Before clicking the button:
After clicking the button:
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:
<!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>
Output:
Before clicking the button:
After clicking the button:
javascript-string
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Differences between Functional Components and Class Components in React
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?