How can I make this regex work on Kotlin/JS the sa...
# getting-started
a
How can I make this regex work on Kotlin/JS the same way it works on Kotlin/JVM?
Copy code
fun main() {
	val r = Regex("""\h+([^:\v]+):""")
    println(r.matchEntire("   Version:")?.groupValues)
    // JVM: [   Version:, Version]
    // JS: SyntaxError: Invalid regular expression: /\h+([^:\v]+):/gu: Invalid escape
}
https://pl.kotl.in/HwXS2Fv3u EDIT: Ahh, never mind.
\h
(horizontal whitespace) doesn’t exist in JS. I’ll try replacing it with
Copy code
[\t\x{00A0}\x{1680}\x{180E}\x{2000}\x{2001}\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200A}\x{202F}\x{205F}\x{3000}]
(as found on https://regex101.com/) further suggestions are welcome!
j
SO suggests
[^\S\r\n]+
to replace
\h
v
An excellent resource when it comes to regex is https://www.regular-expressions.info/. You can for example easily compare the features of different dialects in the reference. On https://www.regular-expressions.info/refshorthand.html you would find the
\h
and see that Java from version 8 on supports it while JavaScript does not. And on https://www.regular-expressions.info/shorthand.html#more you sould then have a elaborate description and alternatives, so the replacement should be
*[*\t\p{Zs}*]
. Unfortunately
\p...
is POSIX class which is also not supported by JavaScript.* 😞 But on https://www.compart.com/de/unicode/category/Zs you can see which characters are part of
Zs
, so your replacement is almost correct, you just miss space and have additionally the "Mongolian Vowel Separator" which is not part of
Zs
.
💯 1
j
ooooh, thank you Björn! I didn't know there was so much to read on this website. It's good to know for later
👌 1
a
thanks @Vampire, that helps. Unfortunately I’m getting tangled up in other regex differences… it seems like
[^\v]
doesn’t work consistently
v
Of course,
\v
is as little supported in JS as
\h
.
[^\v]
probably means either "not a `v`" or "neither
v
nor a backslash"
a
ahh darn, I see
sure would be nice if Kotlin had a common implementation of a Regex engine, so it was the same on all platforms
v
image.png
❤️ 1
😂 1
a
well…. PCRE is written in C. So that could be called from Java and Native, and maybe from JS using wasm?