mrcoles / bookmarklet

A server-side bookmarklet compiler with greasemonkey userscript-like metadata options and the power of babel and uglify

Home Page:https://www.npmjs.com/package/bookmarklet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'\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:

Screen Shot 2019-09-09 at 1 17 58 PM

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.