I would like to match just the root of a URL and not the whole URL from a text string. Given:
http://www.youtube.com/watch?v=ClkQA2Lb_iE
http://youtu.be/ClkQA2Lb_iE
http://www.example.com/12xy45
http://example.com/random
I want to get the 2 last instances resolving to the www.example.com
or example.com
domain.
I heard regex is slow and this would be my second regex expression on the page so If there is anyway to do it without regex let me know.
I'm seeking a JS/jQuery version of this solution.
There is no need to parse the string, just pass your URL as an argument to
URL
constructor:Code:
Result:
oneline with jquery
Try:
*Hit "Run code snippet" to see these methods in action.
Regardless having the protocol or even port number, you can extract the domain. This is a very simplified, non-regex solution, so I think this will do.
*Thank you @Timmerz, @renoirb, @rineez, @BigDong, @ra00l, @ILikeBeansTacos, @CharlesRobertson for your suggestions! @ross-allen, thank you for reporting the bug!
Try this:
If you want to exclude the port from your result, use this expression instead:
Edit: To prevent specific domains from matching, use a negative lookahead.
(?!youtube.com)
in short way you can do like this
Use above function to get domain name