1

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?

3 Answers 3

2

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]);
Sign up to request clarification or add additional context in comments.

Comments

2

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];

Comments

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

1 Comment

Thanks for that link too, looks very helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.