louiscad
07/23/2021, 3:04 PMSyntaxError: 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?ephemient
07/23/2021, 3:13 PMephemient
07/23/2021, 3:16 PM"
, it's not a special characterRobert Jaros
07/23/2021, 3:35 PMRobert Jaros
07/23/2021, 3:36 PM"""\[(?<text>.+)\]\((?<url>[^) ]+)(?: "(?<title>.+)")?\)"""
louiscad
07/23/2021, 11:18 PM