0

I have a simple Regexp with the following code :

<img[^>]+src="([^">]+)"

This regexp allows me to get the src of each img of my document. What I am trying to do without any success, is to add a class condition to it, something that will find all the src of my images which have a certain class. I tried something like that :

<img[^>]+src="([^">]+)" *class="name_of_my_class"

but this only works if the class is inserted after the src, and i need the regexp to work in reverse sens to.

Thanks for any helps !

3
  • Are you only trying to get the src attribute from img tags of a certain class? Why do you need regex to do this? Commented Apr 9, 2013 at 12:28
  • You might be better off using an HTML parser instead - especially if you need to add or modify attributes. For example, I use Ruby and Nokogiri. Using RegEx is not the way to go. Commented Apr 9, 2013 at 12:28
  • My mistake, I forget to precise that I'm parsing a string and not directly the DOM (I parse an Ajax response, before injecting it) Commented Apr 9, 2013 at 12:34

4 Answers 4

2

It's already been said that you shouldn't use regex blablabla...
Still, I can deal with my conscience if I make you learn something about regular expressions at the same time, so, let's do it. The key tool here is the lookahead.

/<img(?=[^>]+class="name_of_my_class")[^>]+src="([^">]+)"/

The lookahead allows you check something ahead without moving the pointer inside your string. So basically, you check something ahead.

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

Comments

1

If you need to traverse the document, searching for classes and returning attributes, you may find it is easier to use the DOM querying tools:

var imgSrcArray = [].slice.call(document.querySelectorAll('img.myClassName'),0)
                       .map(function(img) {
                          return img.getAttribute('src');
                       });

(example assumes a modern browser with querySelector and Array.prototype.map enabled)

2 Comments

Thank for your response @steveukx. The fact is that I'm parsing a string (an ajax response) and not directly the DOM. Can this work ?
Assuming that the string is valid HTML, you can create an element, set the content as the innerHTML and then query on that element: var el = document.createElement('div'); el.innerHTML = myHtmlString; then replace the document in the example above with el.
0

Considering both situations might be a solution, class either before or after the src attribute:

<img[^>]+?((src="([^">]+)"[^>]*?class="name_of_my_class")|(class="name_of_my_class"[^>]*?src="([^">]+)"))[^>]*>

Comments

0

Hmm i dont really know how exactly do this in regexp but i think you can do it as followed :

<img 
    {any charakter except >}* 
    (
        {check for class} {any charakter except >}* {get src} 
        ||
        {get src} {any charakter except >}* {check for class}
    )
    {any charakter except >}*
>

*means 0 or multiple times

Comments

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.