This regex is written to disallow the urls starting with any url scheme and slashes(forward slash, backward slash) but will allow urls like "domain.tld" which are not starting with any url scheme or slashes. It should also allow the strings which are not url("some random input").
^(?!://)((?!//))(?!(.*?)*://)(?!:\\\\)(?!:/\\\\/\\\\)(?!(.*?)*:/\\\\/\\\\)(?!/\\\\/\\\\)(?!\\\\)(?!(.*?)*:\\\\)(?!www.)(?!(.*?)*.www.).*$
This regex works fine in java but in javscript, it is failing for longer strings.
Example: It works fine for "hey. hey hey hey hey" but starts taking time with "hey. hey hey hey hey " and hangs after "hey. hey hey hey hey hey hey"
Following are the cases which should be tested against the regex:
String | Expected result __________________________________________ http://www.google.com | False HTTP://WWW.google.com | False adasd://www.google.com | False ftp://www.google.com | False mailto://www.google.com | False //www.google.com | False ://www.google.com | False www.google.com | False WWW.google.com | False test .http://google.com | False skksdwww.google.com | False wWW.google.com | False ://google.com | False .www.google.com | False as;;; .wwW.google.com | False as.wwW.google.com | False = #$@%@#.www.google.com | False http:/\\/\\google.com | False :/\\/\\google.com | False http://gogle.com | False gogle.com //google.com | False google.com | true some random input | true
What could be the problem in it?
UPDATE: I have updated the regex as per Wiktor Stribiżew's comment and it works fine.
(.*?)*- this is a disastrous subpattern in larger patterns. Use a mere(.*?). Also, if you need to match a literal dot, escape it. See^(?!://)((?!//))(?!(.*?)://)(?!:\\\\)(?!:/\\\\/\\\\)(?!(.*?):/\\\\/\\\\)(?!/\\\\/\\\\)(?!\\\\)(?!(.*?):\\\\)(?!www\.)(?!(.*?)\.www\.).*$.