0

So I'm trying to get all of the possible options from this list with Regex:

<select id="color" name="colorId" class="btn secondary">
<option value="33941">Baltic</option>
<option value="3">Black</option>
<option value="398695">Bright Cherry Red</option>
<option value="172615">Captain Blue</option>
<option value="254777">Chlorophyll Green</option>
<option value="162672">Cosmic</option>
<option value="34237" selected="selected">Guava</option>
<option value="36993">Hawaiian Blue</option>
<option value="172715">Malachite</option>
<option value="9">Navy</option>
<option value="398694">Obscure Blue</option>
<option value="405740">Quartz Purple</option>
<option value="254790">Silver Grey Chine</option>
<option value="231423">Starfruit Yellow</option>
<option value="405844">Tortuga Green</option>
<option value="14">White</option>
</select>

So far I have <select id="color" [^>;]+>\s?<option value="\d+?">(\w+|\W+[^</option>\s?<option value="\d+?">;])</option></select> which of course matches the first option, but I don't know how to get all of them.

suryanaga
  • 3,728
  • 11
  • 36
  • 47
  • 4
    [Please don't use regex to parse HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). – Joseph Silber Jul 19 '13 at 11:59
  • 1
    Please refrain from parsing HTML with RegEx as it will [drive you į̷̷͚̤̤̖̱̦͍͗̒̈̅̄̎n̨͖͓̹͍͎͔͈̝̲͐ͪ͛̃̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) instead. – Madara's Ghost Jul 19 '13 at 12:00
  • **Don't use regular expressions to parse HTML**. You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Jul 19 '13 at 12:01

1 Answers1

1

There's no reason to use a regular expression for this. You can do:

var lis = document.getElementById('color').getElementsByTagName('li');

and then you'll have a nice list of elements to work with.

Bill Criswell
  • 32,161
  • 7
  • 75
  • 66