Skip to content

How to replace all the special character who follows another character

I have a long string containing some special characters. I need to check those special characters who do not follow space and replace it with a special character and space.

Example Input

var a = '<span>< hello < test<zero</span>';


var a = '<span>< hello < test< zero</span>';

Here you can see the last < replaced with < as it follows z which is not space. Keep this in mind in the HTML the special characters are coming in entities like


Regex solution will be great.


let a = '< hello < test<zero';
const pattern = /<(?!s)/g;
let b = a.replace(pattern, "< ");

Description of the pattern:

a ” < ” character that is not followed by a white-space