nerd
nerd copied to clipboard
Unexpected behaviors when using Regular Expressions
Environment
| command | output |
|---|---|
| npx nectar --version | NectarJS v0.7.115 |
| npx --version | 8.1.2 |
| node --version | v16.13.1 |
1. Escaping quotes in regular expressions.
echo '/"/g' > main.js;
npx nectar main.js
gives
SyntaxError: unknown: Unterminated string constant. (1:6) - make sure this is an expression.
> 1 | ("/"/g")
This works if instead of /"/ we use /\x22/
2. Replacing inline regular expression does not work
cat > main.js <<EOF
var x = '"text"';
console.log(x.replace(/\\x22/g, "\\""))
EOF
npx nectar main.js;
./main.js
gives
"text"
Notice that the expected output is \"text\", the same happens if we try to replace the e for something else.
The same is true for new RegExp("\x22", "g")
3 Literal regular expressions produce string
console.log(typeof /e/) // gives string
console.log(typeof new RegExp('e')) // gives object