Need Help Guys (Regex) : I have a regex and it works as expected in All Platforms except Kotlin/JS ...
s
Need Help Guys (Regex) : I have a regex and it works as expected in All Platforms except Kotlin/JS REGEX:
Copy code
"""ytInitialPlayerResponse\s*=\s*(\{.+?\})\;var meta""".toRegex()
*ERROR*(only in Kotlin/JS):
SyntaxError: Invalid regular expression: /ytInitialPlayerResponse\s*=\s*(\{.+?\})\;var meta/: *Invalid escape*
at <global>.new RegExp(<anonymous>)
However this same regex works fine online in a Regex Editor/Tester , link: https://regex101.com/r/WZT1Pr/1/ Any Help would be Appreciated!
r
It's probably connected, because you regexp works fine with Kotlin 1.4: https://youtrack.jetbrains.com/issue/KT-46694
Try not escaping
;
sign
👍 1
s
lemme try
That did the trick, Thanks @Robert Jaros
@Robert Jaros Again Faced Related Issue:
Copy code
"""\{var\s\w=\w\[0];\w\[0]=\w\[\w%\w.length];\w\[\w]=\w\}""".toRegex()
SyntaxError: Invalid regular expression: /\{var\s\w=\w\[0];\w\[0]=\w\[\w%\w.length];\w\[\w]=\w\}/: Lone quantifier brackets
But Online Regex detect it as valid Edit: Fixed It. removed extra \
Copy code
"\\{var\\s\\w=\\w[0];\\w[0]=\\w[\\w%\\w.length];\\w[\\w]=\\w\\}".toRegex()
r
try:
"""\{var\s\w=\w\[0\];\w\[0\]=\w\[\w%\w.length\];\w\[\w\]=\w\}""".toRegex()
s
After Updating to 1.5.31, Again hit this,
SyntaxError: Invalid regular expression: /"\{var\s\w=\w\[0];\w\[0]=\w\[\w%\w.length\];\w\[\w]=\w\}/: Lone quantifier brackets
at <global>.new RegExp(<anonymous>)
at <global>.Regex(/home/shabinder/IdeaProjects/youtube-api-dl/build/js/packages_imported/kotlin/1.5.31/js/src/kotlin/text/regex.kt:56)
at Regex.init(/home/shabinder/IdeaProjects/youtube-api-dl/build/js/packages_imported/kotlin/1.5.31/js/src/kotlin/text/regex.kt:49)
Regex cant be used reliably in multiplatform context I guess.... blob thinking upside down
e
you need to escape the
\]
browser are all strict about JS RegExp syntax when using
/u
Unicode mode, as Kotlin does
s
will that affect JVM or other platforms ? lemme try
e
just use Robert's version, it is already corrected
in this case it should be fine across all platforms, but JS RegExp and Java Pattern aren't the same regex language so many constructs are not portable across platforms
s
Thanks that did the trick again, I forgot try earlier suggestions, But now getting an illegal escape, really JS is quite strict,
Invalid regular expression: /yka=function\(\w\)\{[a-z=\.\(\"\)]*;(.*);(?:.+)\}/: Invalid escape
🤔
e
nothing inside the
[]
character set needs to be escaped
s
Thanks @ephemient this worked
Copy code
"=function\\(\\w\\)\\{[a-z=.(\")]*;(.*);(?:.+)\\}"
Seems like time has come to learn regex myself 🙃
v
I can recommend https://www.regular-expressions.info/ very much as a source for information and also about differences between the different dialects.
👀 1
👍 1
s
https://kotlinlang.slack.com/archives/C3PQML5NU/p1681340732752949 Guys I am back need your help again, this time out of clue why jvm might be failing ?