简体   繁体   中英

Simple javascript regex

I need: www.mydomain.com:1235 form the text var below:

var text = 'http://www.mydomain.com:1235/;image.jpg';

alert(text.match(/\/[^]+\//));

output is: //www.mydomain.com:1235/

How do I exclude the delimiters?

Not a regex, but you could do this:

Example: http://jsfiddle.net/nTmv9/

text = text.split('http://')[1].split('/')[0];

or with a regex:

Example: http://jsfiddle.net/nTmv9/1/

text = text.match(/http:\/\/([^\/]+)\//)[1];

You need to use parens to group what you want to match. Then, the call to .match() will let you use indexers. Index 0 is the whole string match, and index 1 is the first paren grouping.

var text = 'http://www.mydomain.com:1235/;image.jpg';
alert(text.match(/\/([^\/]+)\//)[1]);

This will capture the domain without the http or the url slugs.

https?:\/\/([^\/]+)\/

If you need help figuring out regex here is a great tool I use all of the time.

http://gskinner.com/RegExr/

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM