Skip to content
Advertisement

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>';

Output

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

&lt;

Regex solution will be great.

Advertisement

Answer

let a = '< hello < test<zero';

const pattern = /<(?!s)/g;

let b = a.replace(pattern, "< ");

console.log(b)

Description of the pattern:

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement