0

I'm getting a Javascript minification error saying:

(9,8617-8631): run-time error JS5017: Syntax error in regular expression: /\<br>(.*?)/ig
(9,8757-8776): run-time error JS5017: Syntax error in regular expression: /\<p>(.*?)\<\/p>/ig

The javascript where these occur is:

$htmlPattern =  [
                    /\<br>(.*?)/ig,
                    /\<br\/>(.*?)/ig
                ];

$htmlReplace = [
                    '<p>$1</p>',
                    '<p>$1</p>'
                ];

...and...

$htmlContent = $htmlContent.replace(/\<p>(.*?)\<\/p>/ig, '<div>$1</div>');

Can anyone see what the problem is with the regular expressions noted?

Thanks, Mark

Mark
  • 7,778
  • 24
  • 89
  • 147
  • 1
    Doing justice to the good old maxim ["don't parse HTML with regex"](http://stackoverflow.com/a/1732454/1282023): If you want to replace an element's tagname, you could consider using [jQuery's replaceWith()](http://stackoverflow.com/a/3435890/1282023). Or, if it must be pure JS, have a look at [this function](http://stackoverflow.com/a/4005685/1282023). – MCL Jan 23 '14 at 13:25
  • 1
    Not the problem, but you don't need two patterns. Just make the br's `/` optional, perhaps with optional space also: `/
    (.*?)/ig` or `/
    ]*>(.*?)/ig`
    – Boann Jan 23 '14 at 13:44

1 Answers1

3

If you really wanted that first backslash in those expressions, they must be escaped:

$htmlPattern =  [
                    /\\<br>(.*?)/ig,
                    /\\<br\/>(.*?)/ig
                ];

Unless that was an attempt to escape the <, which is not necessary. In that case, simply remove the backslash:

$htmlPattern =  [
                    /<br>(.*?)/ig,
                    /<br\/>(.*?)/ig
                ];
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98