0

Example: If I have a string segment and need to get all values from width, only from img tags, I don't know how to do it using only one Regex.

By my current knowledge in regex, I would use 3 regexes to do this: First I get all desired HTML tags first (/\<img(.*?)\/?\>/g), then the attribute (/width=\".*?\"/g), and finally, get the content between quotes (/\".[0-9]*?\"/g).

EDITED: Ps.: I'm just asking this for learning purposes in Regex, I'm not planning to parse HTML documents. Currently I get this information using Jquery ($("img").attr('width'))

Marcelo Assis
  • 5,136
  • 3
  • 33
  • 54
  • 1
    Please give us sample input and expected output. – TheLostMind Sep 22 '14 at 13:50
  • 1
    _"I'm just asking this for learning purposes"_ - Well then for learning purposes you should know that trying to use regex to parse whole html documents is not recommended. – nnnnnn Sep 22 '14 at 13:52
  • [Don't parse HTML with regex!](http://stackoverflow.com/a/1732454/418066) – Biffen Sep 22 '14 at 13:53
  • I never planned to parse HTML docs. The whole point of my question was to select only a part of regex to "strip out", but now I figured it out with @vks's answer. Thank you all. – Marcelo Assis Sep 22 '14 at 14:00

1 Answers1

2
\<img.*?\bwidth=\"(.*?)\".*?\/?\>

This will club your regexs together and get you the desired result.

See demo.

http://regex101.com/r/kM7rT8/8

vks
  • 67,027
  • 10
  • 91
  • 124
  • Worked! Now I got it! The parenthesis is like the part of regex that I want to "strip out"? – Marcelo Assis Sep 22 '14 at 13:59
  • 1
    @MarceloAssis yes it will give you the groups you want while the rest of regex matches.you can access it by match.groups or smoething. – vks Sep 22 '14 at 14:00