mathiasbynens / jsesc

Given some data, jsesc returns the shortest possible stringified & ASCII-safe representation of that data.
https://mths.be/jsesc
MIT License
716 stars 48 forks source link

Regular expressions: don’t use `eval` #11

Open mathiasbynens opened 11 years ago

mathiasbynens commented 11 years ago

E.g.

jsesc(/\B/); // '/B/'

The output should be '/\\B/'.

We should get rid of eval and parse any escape sequences in regex.source ourselves.

mathiasbynens commented 11 years ago

https://gist.github.com/WebReflection/6225242 by @webreflection might be of use. For jsesc, the problem is I need to remove any escape sequences from the re.source before I can go ahead and escape everything as needed — but special escape sequences for regular expressions (such as \d etc.) must be preserved.

So, is there a clever way to go from /abc©\xA9/g to /abc©©/g without manually parsing every possible escape sequence?

WebReflection commented 11 years ago
var re = /abc©\xA9/g;
alert(
  JSON.stringify(re.source).replace(
    /\\\\(x[a-zA-Z0-9]{2}|u[a-zA-Z0-9]{4})/g,
    function(m, c){
      return String.fromCharCode(parseInt(c.slice(1), 16));
    }
  )
);
// string abc©©
michaelficarra commented 11 years ago

@WebReflection: Neat idea, but doesn't quite work. Try /abc©\\xA9\xA9/g.

WebReflection commented 11 years ago

Mine was a hint... You can improve that with extra checks/changes ;-) I would have pushed a change otherwise