defsubDomain='(?i:[a-z0-9]|[a-z0-9][-a-z0-9]*[a-z0-9])'// simple regex in single quotesdeftopDomains=$/ (?x-i : com \b # you can put whitespaces and comments | edu \b # inside regex in eXtended mode | biz \b | in(?:t|fo) \b # backslash is not escaped | mil \b # in dollar-slash strings | net \b | org \b | [a-z][a-z] \b )/$defhostname=/(?:${subDomain}\.)+${topDomains}/// variable substitution in slashy stringdefNOT_IN=/;\"'<>()\[\]{}\s\x7F-\xFF/// backslash is not escaped in slashy stringsdefNOT_END=/!.,?/defANYWHERE=/[^${NOT_IN}${NOT_END}]/defEMBEDDED=/[$NOT_END]/// you can ommit {} around var namedefurlPath="/$ANYWHERE*($EMBEDDED+$ANYWHERE+)*"defurl="""(?x: # you have to escape backslash in multi-line double quotes \\b # match the hostname part ( (?: ftp | http s? ): // [-\\w]+(\\.\\w[-\\w]*)+ | $hostname ) # allow optional port (?: :\\d+ )? # rest of url is optional, and begins with / (?: $urlPath )? )"""assert'http://www.google.com/search?rls=en&q=regex&ie=UTF-8&oe=UTF-8'==~urlassert'pages.github.io'==~url
As you can see, there are several notations, and for every subexpression you can choose the one that is most expressive.