How to write a regular expression for URLs
I've been using the Regular Expression Explorer but I still can't come up with the right pattern.
开发者_运维问答Here's my URL:
http://pie.crust.com:18000/TEST/TEST.html
Here's my RegExp:
/[^http:\/\/][\w-\W]+[\/]/
And the output is:
ie.crust.com:18000/TEST/
All I want is the domain (basically everything inbetween // and /):
pie.crust.com:18000
What am I missing? I just can't figure it out. Any ideas?
Thank you in advance.
Try this one: http:\/\/([^\/]+)
The part [^http:\/\/]
is the same as [^htp:\/]
and just enumerates all the characters which shouldn't be in the start part of the resulting string. So for http://pie.crust.com:18000/TEST/TEST.html
http://p
matches this enumeration. I suggest you the following expression:
/http:\/\/([^\/]+)\/.*/
You can use String.replace()
the following way:
var myUrl:String = "http://pie.crust.com:18000/TEST/TEST.html";
var refinedUrl:String = myUrl.replace(/http:\/\/([^\/]+)\/.*/, "$1");
Try this:
@http://+(.*?)/@
(Your regexp doesn't have to start and end with /
- it's easier to use something else that isn't in your search string.
(?<=http:\/\/)[a-zA-Z.:0-9-]+
The p of "pie" is being matched as part of the http rule, and so is not included. Using a positive look-behind fixed this.
http://regexr.com?2uhjf
try this...
//http:\/\/([^\/]+)\/.***/
精彩评论