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

How to replace all the special characters following another character – JavaScript?


Let’s say the following is our string with special characters −

var sentence = '<My<Name<is<John<Doe';

We are replacing the special character and setting a space with it using replace() along with regex.

Example

Following is the code −

var sentence = '<My<Name<is<John<Doe';
var regularExpresion = /<(?!\s)/g;
var result = sentence.replace(regularExpresion, "< ");
console.log(result);

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

node fileName.js.

Here, my file name is demo271.js

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo271.js
< My< Name< is< John< Doe