I am trying to make the following unicode regular expression work in nodejs, but all I get is an invalid escape error. I can't figure out, what to escape here or if this for some reason doesn't work at all in node. This is my original regex:
/([\p{L}|\-]+)/ug
If I escape the \p like \\p, the regex doesn't work anymore (outputs only p,L and -)
This works in chrome, so it should work in node somehow too, right? Thanks for your help.
var str = "thÛs Ís spå-rtÅ!";
console.log(str.match(/([\p{L}|\-]+)/ug))
A quick look through the nodejs changelog revealed this PR:
https://github.com/nodejs/node/pull/19052
which most notably states:
RegExp Unicode Property Escapes are at stage 4 and will be included in ES2018. They are available since V8 6.4 without a flag so they will be unflagged in Node.js v10. They are also available under the --harmony_regexp_property flag in Node.js v6-v9 and under the --harmony flag in Node.js v8-v9.
So by the look of it, if you are on node v6-v9, you can enable this feature by running node with a flag. For example, this works for me on node v8.11.3:
node --harmony regex-test.js
(where regex-test.js contains your sample code). Running this without the flag gives your Invalid escape error.
If you can update your node version to v10+, no flag is needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With