How to globally replace a forward slash in a JavaScript string ?
In this article, we will see how to globally replace a forward slash in a JavaScript string. We have a few methods to do this which are described below:
- Using replace() method with a regular expression
- Splitting in place of the forward-slash and joining it back with the required 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 a 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: This example shows the above-explained approach.
html
< 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 > |
Output:

Globally replace a forward slash in a JavaScript string
Method 2: Splitting in place of the forward-slash and joining it back with the 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: This example shows the above-explained approach.
html
< 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 > |
Output:

Globally replace a forward slash in a JavaScript string
Please Login to comment...