deepesh deepesh - 1 year ago 76
Javascript Question

Get a text pattern between the string in javascript

This is the HTML text



<select name="ctl00$ContentPlaceHolder1$ddlyear" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$ddlyear\',\'\')', 0)" id="ctl00_ContentPlaceHolder1_ddlyear" class="page-heading" style="background-color: rgb(255, 244, 244); width: 95px; outline: 1px solid blue;">
<option selected="selected" value="Select Year">Select Year</option> <option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
</select>





From this I'm trying to extract the years like frm 2000 till 2017, so for this I'm first doing a regex pattern match between string "" and then getting all the numbers from it but my regex is getting wrong.

var regex = /<option selected(.*)</option>/;
var newtext = \"{{text}}\".replace(regex,'')


The first step to get the years is itself getting failed.
Any suggestion where it is getting wrong?
Any help would be much appreciated.Thanks

Answer Source

It's way more easier by directly querying the select element and grabbing its option values than messing with plain HTML:

var yearsArray = [];

var options = document.getElementById('ctl00_ContentPlaceHolder1_ddlyear').options;

// i = 1, because we don't want the option[0], which is "Select Year"
for(var i = 1; i < options.length; i++) {
	yearsArray.push(options[i].value);
}

console.log(yearsArray);
<select name="ctl00$ContentPlaceHolder1$ddlyear" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$ddlyear\',\'\')', 0)" id="ctl00_ContentPlaceHolder1_ddlyear" class="page-heading" style="background-color: rgb(255, 244, 244); width: 95px; outline: 1px solid blue;">
  <option selected="selected" value="Select Year">Select Year</option>
  <option value="2000">2000</option>
  <option value="2001">2001</option>
  <option value="2002">2002</option>
  <option value="2003">2003</option>
  <option value="2004">2004</option>
  <option value="2005">2005</option>
  <option value="2006">2006</option>
  <option value="2007">2007</option>
  <option value="2008">2008</option>
  <option value="2009">2009</option>
  <option value="2010">2010</option>
  <option value="2011">2011</option>
  <option value="2012">2012</option>
  <option value="2013">2013</option>
  <option value="2014">2014</option>
  <option value="2015">2015</option>
  <option value="2016">2016</option>
  <option value="2017">2017</option>
</select>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download