7 URL regular expressions to retrieve all parts of URL

Quick syntax reference

flags
  • g - global match
  • i - ignore case
  • m - match over multiple lines
Escaping
  • \ - special characters to literal and literal characters to special
Quantifiers
  • ? - matches zero or one times
  • * - matches zero or more times
  • + - matches one or more times
  • {n} - matches n times
  • {n, m} - matches at least n times, but not more than m times
Anchors
  • ^ - matches at the start of the line
  • $ - matches at the end of the line
  • \b - matches at the beginning or the end of a word
delimiter
  • (?:x) - matches x not remember the match
  • x(?=y) - matches x only if x is followed by y
  • x(?!y) - matches x only if x is not followed by y
Character Escapes
  • \s - matches whitespace
  • \S - matches anything but a whitespace
  • \f - matches a form-feed
  • \n - matches a linefeed
  • \r - matches a carriage return
  • \t - matches a horizontal tab
  • \v - matches vertical tab
  • \w - matches any alphanumeric character including the underscore. Equivalent to [A-Za-z0-9_]
  • \W - matches any non-word character. Equivalent to [^A-Za-z0-9_]
Others
  • . - matches any character except a newline
more on regular expression

Following the last post regular expressions on HTML tags. 7 regex on url were created. It should be enough for my use.

1
Using match to retrieve all values of parameters. Remove = with replace(/=/,"").
Using split to retrieve all names of parameters. Remove & with replace(/&/,"").
RegEx Expression:
/(?:=)[^&]*/g
Method:
match,split
Testing String
http://www.pagecolumn.com/tool/regtest.htm?rp=<\s*h4[^>]*>(.*?)<\s*/\s*h4>&rf=g&me=2&ts=sdfs
Live Test
2
Using match to retrieve address part of url
Using split to retrieve parameter part of url
RegEx Expression:
/.*?(?=\?)|.*/
Method:
match
Testing String
http://www.pagecolumn.com/tool/regtest.htm?rp=<\s*h4[^>]*>(.*?)<\s*/\s*h4>&rf=g&me=2&ts=sdfs
Live Test
3
matches file name
RegEx Expression:
/[\w_.-]*?(?=\?)|[\w_.-]*$/
Method:
match
Testing String
http://www.pagecolumn.com/tool/regtest.htm?rp=<\s*h4[^>]*>(.*?)<\s*/\s*h4>&rf=g&me=2&ts=sdfs
Live Test
4
matches file name extension
RegEx Expression:
/[\w]*?(?=\?)|[\w]*$/
Method:
match
Testing String
http://www.pagecolumn.com/tool/regtest.htm?rp=<\s*h4[^>]*>(.*?)<\s*/\s*h4>&rf=g&me=2&ts=sdfs
Live Test
5
matches file name, remember name part and extension
RegEx Expression:
/([\w_-]*)\.(\w*)?(?=\?)/
Method:
exec, match
Testing String
http://www.pagecolumn.com/tool/regtest.htm?rp=<\s*h4[^>]*>(.*?)<\s*/\s*h4>&rf=g&me=2&ts=sdfs
Live Test
6
matches website address, remember domain name.
RegEx Expression:
/\w*:\/\/w*\.?([\w-_]*\.?[A-Za-z]*):?\d*\//
Method:
exec, match
Testing String
http://www.pagecolumn.com/tool/regtest.htm?rp=<\s*h4[^>]*>(.*?)<\s*/\s*h4>&rf=g&me=2&ts=sdfs
Live Test
7
matches the whole url, remember url address, names and values of all parameters. Add the string [\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)? if you have more parameters.
RegEx Expression:
/(?:\w*:\/\/)?((?:[\w-_]*\.?)+:?\d*(?:\/?[\w-_.]+\/?)*)[\?]?[\&]?([\w- _]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?[\&]?([\w-_]*)?=?([\/\w-_\?\)% \.\(\*\[\]\^<>\\]*)?[\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?[\&] ?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?/
Method:
exec, match
Testing String
http://www.pagecolumn.com/tool/regtest.htm?rp=<\s*h4[^>]*>(.*?)<\s*/\s*h4>&rf=g&me=2&ts=sdfs
Live Test

Social Bookmark if they are useful.