Unicode regex \p{L} not working in NodeJS - javascript

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 are going to use --harmony flag please consider this
As mentioned in the Node Documentation, --harmony flag enables the non-stable but to be soon stable features of ES6
The current behaviour of the --harmony flag on Node.js is to enable staged features only. After all, it is now a synonym of --es_staging. As mentioned above, these are completed features that have not been considered stable yet. If you want to play safe, especially on production environments, consider removing this runtime flag until it ships by default on V8 and, consequently, on Node.js. If you keep this enabled, you should be prepared for further Node.js upgrades to break your code if V8 changes their semantics to more closely follow the standard.
here is the link for that
https://nodejs.org/en/docs/es6/#:~:text=The%20current%20behaviour%20of%20the,to%20enable%20staged%20features%20only.&text=If%20you%20want%20to%20play,js.

Related

Why are unicode property escapes throwing "Unknown property" errors?

The MDN website gives examples of matching patterns with unicode support, e.g.
const sentence = 'A ticket to 大阪 costs ¥2000 👌.';
const regexpCurrencyOrPunctuation = /\p{Sc}|\p{P}/gu;
console.log(sentence.match(regexpCurrencyOrPunctuation));
It works fine on stackoverflow as a snippet.
However, in a javascript codesandbox, the code throws an error:
/src/index.js: Unknown property: Sc
In a Next.js codesandbox it also throws the same error.
On the other hand, on regex101 website the pattern is correctly matched to the sentence, with ECMAScript flavor and with "gu" flag.
Additionally, in my real world Next.js Typescript project, a pattern /\P{L}/gu worked fine until yesterday when I upgraded all dependencies to latest versions. Now it throws similar error with strict mode set to true in tsconfig.json. With strict mode set to false it still works fine.
Why is this error occurring and how to use the /\p{Sc}|\p{P}/gu or /\P{L}/gu regex pattern in code?
Based on the documentation, Sc is a non-binary property. Which means you can't just use \p{Sc}; you have to use \p{Sc=some_script_name}, where the script name is taken from here.
Unfortunately, it's a bug in next.js: https://github.com/vercel/next.js/issues/19303

Path separator for Atom / JavaScript on Windows

I have developed an Atom package which calls SyncTeX (a utility for reverse lookup for LaTeX), and then opens in Atom the file specified in the SyncTeX response. I'm developing on Linux, but now a user on Windows tells me that this doesn't work.
In detail: SyncTeX returns a pathname like C:\data\tex\main.tex, with Windows-appropriate backslash separators. My package then calls atom.workspace.open with exactly that returned string, which leads to a JavaScript error (file not found).
Strangely, if the string is modified to use forward slashes instead, C:/data/tex/main.tex, the call works and the file is opened.
My questions:
Is this behavior specific to Atom, or to some underlying technology (JavaScript, Electron, Node, ...)? I was unable to find any documentation on this.
Since the replacement \ → / is apparently necessary, is there a preferred way to implement it? Would a simple String.replace be adequate?
Do I risk breaking compatibility with other platforms if I always do the replacement?
By my best knowledge paths with forward slashes '/' work well everywhere except Windows XP.

Getting this error while trying BDD cucumber Selenium when designing Page object model

Exception in thread "main" cucumber.runtime.CucumberException: java.lang.AbstractMethodError: cucumber.runtime.java.picocontainer.PicoFactory.addClass(Ljava/lang/Class;)V
at cucumber.runtime.java.JavaBackend.addStepDefinition(JavaBackend.java:154)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:68)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:41)
at cucumber.runtime.java.JavaBackend.loadGlue(JavaBackend.java:86)
at cucumber.runtime.Runtime.(Runtime.java:91)
at cucumber.runtime.Runtime.(Runtime.java:69)
at cucumber.runtime.Runtime.(Runtime.java:65)
at cucumber.api.cli.Main.run(Main.java:35)
at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.lang.AbstractMethodError: cucumber.runtime.java.picocontainer.PicoFactory.addClass(Ljava/lang/Class;)V
at cucumber.runtime.java.JavaBackend.addStepDefinition(JavaBackend.java:149)
... 8 more
The problem is that cucumber cannot find your glue libraries. The commandline runner is in the stack trace which suggests that you are not following the syntax rules for CLI. Here is an example:
mvn clean test -Dcucumber.options="--tags #search --monochrome --plugin pretty:STDOUT --plugin html:target/cucumber-html-report --plugin json:target/cucumber.json --glue steps --glue runsupport classpath:features"
Note the double dash characters before keywords. Also notice that since there are two glue paths that there are two --glue clauses. Also note that only the package name of the two -glue paths were specified.
Further note that STDOUT needed to be specified on --plugin pretty:STDOUT.
Finally note that the features keyword was dropped completely. The path specified at the end (without a keyword) tells cucumber-jvm where to find the feature files.
Be warned, if you get any of this wrong then cucumber-jvm gives you cryptic error messages with which you have first hand knowledge. The usage is explained here.
BTW, claspath: refers to, in this case, the Maven default classpath which for features is
src/test/resources/
If you want help in the future post the minimum code necessary to reproduce the problem. Remember that your brain is not a network connected device. :-)

Using String.raw() with Node JS

I'm working on a Node.js app with and I would like to use String.raw() which is part of the ES 6 standard.
However, when using it as in the documentation:
text = String.raw`Hi\n${2+3}!` + text.slice(2);
It returns SyntaxError: Unexpected token ILLEGAL for the character after String.raw.
I think that there is a problem because String.raw() is a new technology only available for Chrome and Firefox yet. However, can I use it in Node.js and how?
The grave character after raw denotes template strings, which is a feature in ES6 Harmony. You can invoke node with --harmony flag, but this feature is not yet implemented. This is the reason of the syntax error. Raw strings are unsupported too.
If you want experimenting with this feature in server side, check out io.js, which is a fork of node, but with many ES6 features implemented and enabled by default.

Flagging comma at last element of array as an error for JavaScript/Eclipse

I have a Javascript project that must be compatible with older versions of IE, and I am using Eclipse Juno as my IDE.
Older versions of IE cannot handle a comma after the last element of an array, even though this is correct ECMAScript:
[a,b,c,]
Unfortunately, though this is correct syntax, it breaks my application only for IE, and only in production (where backwards compatibility is forced), and in a way that is very hard to debug (it does not fail anywhere near the line that is wrong).
Is there a way to set Eclipse to flag this as an error with the syntax validator? I did not see this as an option under Preferences -> JavaScript -> Validator -> Errors/Warnings
I would warmly recommend installing jshint for eclipse which would analyze your code, and provide insightful reports about your code (including the problem you reported)
If you're not sure, just give jshint a try. Paste your code, press lint, and get the results.
Example
For the code:
var arr = [1,2,3,];
You get:
Line 1: var arr = [1,2,3,];
Extra comma. (it breaks older versions of IE)

Categories