Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regular expressions: don’t use eval #11

Open
mathiasbynens opened this issue Aug 1, 2013 · 4 comments
Open

Regular expressions: don’t use eval #11

mathiasbynens opened this issue Aug 1, 2013 · 4 comments

Comments

@mathiasbynens
Copy link
Owner

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 added a commit that referenced this issue Aug 3, 2013
It may return at some point in the future.

Ref. #10 and #11.
@mathiasbynens
Copy link
Owner Author

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
Copy link

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
Copy link

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

@WebReflection
Copy link

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

mathiasbynens added a commit that referenced this issue May 25, 2014
It may return at some point in the future.

Ref. #10 and #11.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants