0

okay I give up. Here's my code:

var re = /href="(http.*\.jpg)"/g;
var mp3s = body.match(re);

it finds pictures, but it returns href="http://www.picture.com/smthg.jpg"

instead of returning http://www.picture.com/smthg.jpg

any idea why?

7
  • Because you're including the href attribute in the match Commented Apr 23, 2014 at 23:22
  • When you have access to the DOM, why bother using something so primitive as regular expressions? It seems pretty naive. Commented Apr 23, 2014 at 23:23
  • because it is faster than importing a dom parser library. This really doesn't answer my question. The regex seems fine to me I don't see where is the problem... I capture the group excluding the href Commented Apr 23, 2014 at 23:25
  • If you want to make sure that there is a "href=" before your link but not catch it, look up "lookbehind regular expression" in Google, it will do just that. Commented Apr 23, 2014 at 23:29
  • lookbehind is not supported in javascript Commented Apr 23, 2014 at 23:32

3 Answers 3

2

The result from match() is actually an object.

I think you need to access the first element on that object.

For example:

body.match(re)[1]

This is where the actual result is kept.

Shameless self-promotion: I've written a small guide for me, I can never remember how to use these either. It's here: http://queirozf.com/reminders/javascript-regular-expressions-usage-reminder

Sign up to request clarification or add additional context in comments.

4 Comments

It's not really a self-promotion if you didn't promote yourself - where's your guide at? Can we see it? :)
@Zlatko =) ok then there you go.
I console.log it and I only get one result, the one with the entire thing and not just the capture group.
Now it's a self-promotion :) Though not a shameless one, I think these short reminders are excellent and they can also help out others like this.
0

try

var re = /(http.*\.jpg)/g;
var mp3s = body.match(re);

since you don't need the href.

1 Comment

yeah but I want to be sure that it is in a link, not like that.
0

You want to match the regular expression, but then return just the portion in brackets.

To do this, call the regular expressions exec method. For example:

var body = 'stuff stuff morestuff href="http://www.picture.com/smthg.jpg" and some more stuff';
var re = /href="(http.*\.jpg)"/g;
var regexResults = re.exec(body);
var mp3s = regexResults[1];
alert(mp3s);

Having given you this answer, I must implore you to find a different way to solve this problem. You cannot parse HTML using regular expressions. No matter how sophisticated your regular expression gets, there will be a legal HTML example which will break it.

1 Comment

do you have a quicker solution when I just need to get .jpg ? A entire dom parser seems too heavy to me.

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.