Open mathiasbynens opened 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?
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©©
@WebReflection: Neat idea, but doesn't quite work. Try /abc©\\xA9\xA9/g
.
Mine was a hint... You can improve that with extra checks/changes ;-) I would have pushed a change otherwise
E.g.
The output should be
'/\\B/'
.We should get rid of
eval
and parse any escape sequences inregex.source
ourselves.