0

This might sound weird, but i want to parse a xml response by pushing certain string with following regular expression:

data-href="[\d\w\/:\.\=]*[">]? 

into an array.

The reason for this is just for testing. A friend has built a webpage with jimdo where he displays a image gallery. now i want to try parsing the xml on this site and only fetch the images which are at every data-href tag and use them in my react native app.

Any ideas?

1 Answer 1

1

Why not just use the String.match method?

var matches = xml_str.match(/data-href="[\d\w\/:\.\=]*[">]?/g);

Edit: if you want to just get the URL within the href, use regex capture groups:

var matches = xml_str.match(/data-href="([\d\w\/:\.\=]*)[">]?/g);
console.log(matches);

You can then map the matches array to only contain the element with the URL.

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

5 Comments

Thanks mate. But i am doing something wrong in my code. If i test the xml response with regex101.com it looks great, but if i do this in my code i get a weird match response. Maybe it is correct, but how would u push every "data-href" in the response into a array?
Sorry, you have to put a global flag after the end delimiter in JS. I updated my answer. If you're still having trouble, save and post your regex101 link so I can check it out.
Thanks bro. It works here https://regex101.com/r/c3yiYX/2, but not in my code. I get null in my code now
thanks mate. i updated by removing /d and then i just saw that i am not allowed to use the regEx in a string which i should have seen before!!! .match(/data-href="([\d\w\/:\.\=]*)[">]?/g); not .match("/data-href="([\d\w\/:\.\=]*)[">]?/g");
@BigPun86—yes you can, just quote the quotes, e.g. .match(/data-href=\"([\w\/:\.\=]*)[\">]?/g);. Posting code as a runnable snippet helps ensure that it actually works. ;-)

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.