Regular expression syntax
yrns opened this issue · 6 comments
If you use regular expression syntax in the search patterns, you quickly start getting undefined values back as replacements. It's crafty to use a single RegExp and use the patterns as keys to store the replacements, but the returned matches don't necessarily equal the patterns unless they are simple strings. As an example which bit me: searching for require('path')
requires escaping the parens and quotes, but then the replacement is stored under a key like 'require\\(\'path\'\\)'
when the match returned is simply require('path')
, which isn't a key in values
.
I searched for a way to find out what part of the alternation pattern was matched, but I couldn't find anything easy. Otherwise I would have submitted a fix. Maybe using named groups (e.g. https://github.com/edvinv/named-js-regexp) or simply using multiple regular expressions would work. The latter might be slower, but more straightforward.
Or disallow regular expressions and remove any escape sequences in the keys. But that wouldn't be as potentially useful!
I think the simple and intuitive solution would be to escape all rexeg meta characters before passing values
to RegExp. Seeing as the escape method already exists, this should be relatively simple. Thoughts?
for now, I think I'll just go with this Babel transform, which dose the same thing as seems to be way quicker
Yeah, I immediately got bitten by the fact that the strings to replace are actually regexps. That's not intuitive.
@Rich-Harris are the values to be replaced supposed to be regexes? I don't see anything about that in the docs; to the contrary, the usage seems to suggest that they should be literal values. So, I think this line should be
- const pattern = new RegExp( delimiters[0] + '(' + Object.keys( values ).join( '|' ) + ')' + delimiters[1], 'g' );
+ const pattern = new RegExp( delimiters[0] + '(' + Object.keys( values ).map(escape).join( '|' ) + ')' + delimiters[1], 'g' );
This is (finally) implemented in 1.2
Not being able to use regular expression is a bit too restrictive IMHO... This could be really useful in some scenarios.