'\w' gets transpiled to 'w'
NoNoNo opened this issue · comments
Source:
console.log('\w');
Expected result:
console.log('\w');
Actual result:
console.log('w');
Thanks & greetings!
Hey @NoNoNo I think '\w' and 'w' are the same things in javascript?
For example, take a look at this:
thanks @mrcoles! It was a oversimplified test case.
I used to construct a regex with new RegExp()
like
console.log(new RegExp('\w'));
So my problem still persists…
Oh, I’m seeing the issue here. If you were doing it as a regex literal then you could write it as /\w/
, but, since you’re passing a string into RegExp, you need to double-escape the "w" like this: new RegExp('\\w')
.
> '\w' === 'w'
true
> '\\w' === 'w'
false
I do love regular expressions, but I am reminded of this XKCD. Relatedly, I’ve learned that any functions with regular expressions in them should pretty much always have associated unit tests, since they’re so easy to mess up.