Invalid Regex Group

Can anyone understand why this gives an error Invalid regular expression: Invalid group?

text.replace(/(?<!br|p|\/p|b|\/b)>/g, "&gt;");

This is normal:

text.replace(/<(?!br|p|\/p|b|\/b)/g, "&lt;");

So I'm not sure where I am wrong in the first ( &gt;).

Here's a fiddle with an example.

+3
source share
1 answer

JavaScript does not support lookbehinds. Here is one way to achieve the same behavior:

text = text.replace(/(br|p|\/p|b|\/b)?>/g, function($0, $1){
    return $1 ? $0 : "&gt;";
});

This approach comes from the following blog post: Mimicking Lookbehind in JavaScript

Below is the updated fiddle .

+3
source

All Articles