Skip to content Skip to sidebar Skip to footer

Javascript Regex To Match A Pattern But NOT Match A Regex Literal (r.js Optimizer And Uglify Issue)?

I've got a Backbone application, organized into modules using Require.js. One of these modules contains a Handlebars helper, which has a method I use to pull a legal header off of

Solution 1:

Change your regexp to:

var htmlCommentRegex = /[<]!--[\s\S]*?-->/g;

The single-character [<] class is equivalent to < as far as the RE processor is concerned, but now the RE no longer matches itself.

Another way is to escape one of the literal characters in the RE:

var htmlCommentRegex = /<\!--[\s\S]*?-->/g;

Or you could build the RE from strings:

var htmlCommentRegex = new RegExp('<!'+'--[\s\S]*?-->', 'g');

If r.js is optimizing all these back to the original text, try this:

var commentPrefix = '<!';
var htmlCommentRegex = new Regexp(commentPrefix+'--[\s\S]*?-->', 'g');

Hopefully it doesn't do enough code analysis to undo this obfuscation.


Post a Comment for "Javascript Regex To Match A Pattern But NOT Match A Regex Literal (r.js Optimizer And Uglify Issue)?"