Regular expression to retrieve parameters inside select tag

I need to extract options into a 'select' special tag. Is it possible to use a single regex, or do I need to grab the inner html of the selection first and then the parameters? Here's an example html:

<select id="select_id">
  <option selected value="">Select Type</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
</select>

      

.....

Thank.

0


source to share


2 answers


While it is possible to create a regex that will do what you want, I really want you to be happier doing it through the DOM unless you have a reason not to use the DOM. There are no tags that suggest language or platform, so more specific information is difficult to get.

Any specific reason for trying to parse HTML with a regular expression rather than loading it into the DOM, or using the DOM available in the browser via Javascript?

If you only have a snippet like this, you can use

value="(\d*)"

      



Where (\ d *) will write the values โ€‹โ€‹of each option.

The problem I see is that you would need to narrow your search through a different regex to get such a simple query. Something like

<select.*>(.*?)</select>

      

in the outer loop will work in most cases. However, the DOM is your friend and avoids such hacks.

+1


source


I would look for DOM library support, but if you need to do something similar to this:

"<select.*?>.*?<option value=\"(\d+)\">" + select_id + "</option>.*?</select>"

      



Where select_id

is the option selection. Also, make sure multiline scan support is enabled.

0


source







All Articles