JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to remove percentage symbol from string in JavaScript?

To remove the percentage symbol from string, call the replace() method and pass /%/g and empty string '' as parameters. The replace() method will return a new string in which all the percentage symbols are removed.

let str = 'I%am%learning%JavaScript';
str = str.replace(/%/g, '');
console.log(str); //"IamlearningJavaScript"

The String.replace() method accepts two parameters:

  1. The first parameter can be a regular expression or a string. It is the pattern or string that you want to replace.
  2. Replacement string or a function. It will be called for every match.

If you pass a string as a first argument to the replace() method, it replaces the first occurrence of that string. To remove all percentage symbols in a string, pass a regular expression with the g flag.

Recommended Posts