Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptHow to globally replace a forward slash in a JavaScript string ?

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:

Methods to globally replace forward slash:

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.

Javascript




// Input string
let origString = 'string / with some // slashes /';
  
// Disply
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 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.

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


the

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 :

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 *

RELATED ARTICLES

Most Popular

Recent Comments