Open In App

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

Last Updated : 15 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, a forward slash serves multiple purposes such as division operator, string character, etc. In this article, we will see how to globally replace a forward slash in a JavaScript string.

Below are the methods used to globally replace a forward slash in a JavaScript string:

Method 1: Using replace() method with a regular expression.

  • The replace() method searches a string for a specified value or expression, replacing it with a new value. The original string remains unchanged.
  • To replace all forward slashes, a regular expression is used. The forward slash (/) is a special character in regex, necessitating escape with a backward slash ().
  • The global modifier (g) in the regular expression ensures the replacement of all occurrences of forward slashes in the given string.
  • Importantly, the replace() method does not modify the original string; it returns a new string with replacements.

Syntax:

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

Example: In this example, we are using the JavaScript replace() method.

Javascript




// Input string
let origString = 'string / with some // slashes /';
 
// Display
console.log(origString);
 
// Replacement for slash
let replacementString = '*';
 
// Replaced string
let replacedString = origString.replace(/\//g, replacementString);
 
// Display output
console.log(replacedString);


Output

string / with some // slashes /
string * with some ** slashes *

Method 2: Using the JavaScript split() method

  • The split() method breaks a string into parts using a chosen separator, such as the forward slash. This creates an array of strings based on where the slashes were.
  • The join() method then combines an array of strings back into one. By specifying a new character as a parameter, it replaces all the original forward slashes in the initial string.
  • Together, split() and join() offer a straightforward way to replace specific characters, like the forward slash, in a given string.
  • It’s crucial to note that these operations don’t change the original string; instead, they produce a new string with the desired modifications.

Syntax:

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

Example: In this example, we are using JavaScript split() method.

Javascript




// Input String with slashes
let origString = 'string / with some // slashes /';
 
// Display input string
console.log(origString);
 
// Replacement for slash
let replacementString = '*';
 
// Replaced String
let replacedString =
            origString.split('/').join(replacementString);
             
// Display output
console.log(replacedString);


Output

string / with some // slashes /
string * with some ** slashes *

Method 3: Using JavaScript replaceAll() method

The JavaScript replaceAll() method returns a new string after replacing all the matches of a string with a specified string or a regular expression. The original string is left unchanged after this operation.

Syntax:

const newString = originalString.replaceAll(regexp | substr , newSubstr | function);

Example: In this example, we are using JavaScript replaceAll() method.

Javascript




// Input string
let origString = 'string / with some // slashes /';
// Display input string
console.log(origString);
 
// replacement cahracter
let replacementString = '*';
 
// Replace all slash using replaceAll method;
let replacedString =
            origString.replaceAll('/', '*');
 
// Display output
console.log(replacedString);


Output

string / with some // slashes /
string * with some ** slashes *


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads