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:
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.