Computer >> Computer tutorials >  >> Programming >> Javascript

JavaScript regex - How to replace special characters?


To replace special characters, use replace() in JavaScript.

The syntax is as follows −

anyVariableName.replace(/(^\anySymbol)|,/g, '');

Example

Following is the code −

var values = '&677,431,444,98777';
var result=values.replace(/(^\&)|,/g, '');
console.log(result);

To run the above program, you need to use the below command −

node fileName.js.

Here, my file name is demo319.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo319.js
67743144498777