Hello, I have a regex made to match markdown links that works perfectly in Kotlin/JVM, but in JavaScript, it leads to a runtime error:
SyntaxError: Invalid regular expression: /\[(?<text>.+)\]\((?<url>[^) ]+)(?: \"(?<title>.+)\")?\)/: Invalid escape
at new RegExp (<anonymous>)
Here's the source code that works in the JVM and fails in Chrome :
//language=RegExp
val linksRegex = """\[(?<text>.+)\]\((?<url>[^) ]+)(?: \"(?<title>.+)\")?\)""".toRegex()
When I look at the generated JavaScript code, here's what I get:
var tmp0_toRegex_0 = '\\[(?<text>.+)\\]\\((?<url>[^) ]+)(?: \\"(?<title>.+)\\")?\\)';
I tried to workaround using
js("…")
:
val linksRegex = js("""'\[(?<text>.+)]\((?<url>[^) ]+)(?: "(?<title>.+)")?\)'""").unsafeCast<String>().toRegex()
Unfortunately, it then seems to strip the backslashes away as it's matching quite random strings that are not markdown-style links at all but plain English sentences.
Is there a way to get that Regex to work in JS like it does in the JVM?