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.
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 −
Here, my file name is demo271.js
This will produce the following output on console −