3

I would like to remove the years 2014 & 2015 from both the check in and check out using Regex-

enter image description here

I tried using containsRegex but I guess I'm doing something wrong:

$("option:containsRegex(/^((2014)|(2015))$/)").remove();

Here's the example code:

<select name="check_in" class="required">
    <option value="">Year</option>
        <option value="2013" selected="selected">2013</option>  
        <option value="2014">2014</option>
        <option value="2015">2015</option>
        <option value="2016">2016</option>
</select>

<select name="check_out" class="required">
    <option value="">Year</option>  
        <option value="2013" selected="selected">2013</option>  
        <option value="2014">2014</option>
        <option value="2015">2015</option>   
        <option value="2016">2016</option>
</select>

I can do it like that:

$('option:contains("2014")').remove();
$('option:contains("2015")').remove();

But I want to be able to do it using regex becuase the real file is more complicated than that.. Here's the fiddle: http://jsfiddle.net/PJjGb/1/ so you can try.

To sum it up I want to remove certain 'option's of the 'select' element. and to be able to choose which option by using regex- In this example removing option '2014' and '2015' using regex from both check in and check out. Thank you. (Here I did that without regex: http://jsfiddle.net/PJjGb/)

Community
  • 1
  • 1
Roko
  • 1,233
  • 1
  • 11
  • 22

2 Answers2

5

Not sure why you need a regex, but here it is :

$('option').filter(function() {
    return /(2014|2015)/.test(this.value);
}).remove();

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Not that it matters, but since the OP is using `:contains()`, I'm thinking they want to test against `$(this).text()`, not `this.value` (although the same in their code). It's up to them; your answer demonstrates how to filter dynamically, and with a regex – Ian Jul 23 '13 at 15:04
  • @adeneo ye it works, Thank you. Do any of you maybe know how to make it work using containsRegex? Like I have seen here: http://bit.ly/1bJkLaE – Roko Jul 23 '13 at 15:08
  • @Ian - agreed, `contains()` would equal a regex test on the textContent, but `this.value` seemed easier to write as it's the same thing in the code in the question. If the text is really what the OP wants, it's easy to change. – adeneo Jul 23 '13 at 15:20
  • @BooyaRin Might I suggest you test your regex expressions at http://regexpal.com/. It's a quick and easy way to make sure you Regex expressions are getting to what you want. Otherwise, you might also look at the CSS Selectors link in my answer, tho i'll prolly delete it soon, since everyone seems to think i ddn't answer your question. – SpYk3HH Jul 23 '13 at 15:24
  • @SpYk3HH The regexpal.com I'm familiar with. The CSS Selector link was usefull, Ty. – Roko Jul 23 '13 at 15:40
  • `Not sure why you need a regex` Isn't it better for your digestion? +1 :) – zx81 Jun 09 '14 at 00:30
-1

The regex itself should be as simple as /(2014|2015)/


Why use the regex when it's just a simple jQuery call using simple css, like so:

$('option[value*=2014], option[value*=2015]').remove();

Example


Learn more about CSS Selectors

  • [attribute*=value]: attribute contains value
  • [attribute^=value]: attribute begins with value
  • [attribute$=value]: attribute ends with value
  • [attribute~=value]: attribute is within space separated list
  • [attribute|=value]: attribute is within dash separated list

Or as also suggested by adeneo, if you insist on RegEx, you could use jQuery's .filter() method:

$('option').filter(function(i) { return /(2014|2015)/.test(this.value); }).remove();

Example



|OR| If you're wanting to test against the text:

$('option:contains(2014), option:contains(2015)').remove();

Example

|OR| using .filter()

$('option').filter(function(i) { return /(2014|2015)/.test($(this).text()); }).remove();

Example



|OR| For a Full Combo testing against TEXT && VALUE, I would suggest using the .filter() method agian, with a || statement:

$('option').filter(function(i) { return /(2014|2015)/.test(this.value) || /(2014|2015)/.test($(this).text()); }).remove();

EXAMPLE

Community
  • 1
  • 1
SpYk3HH
  • 22,272
  • 11
  • 70
  • 81
  • 4
    From the Question..."becuase the real file is more complicated than that" – CodingIntrigue Jul 23 '13 at 14:51
  • 1
    The value property and value attribute are not the same thing – Esailija Jul 23 '13 at 15:00
  • 1
    @Esailija if the value prop is changed after DOM load, sure, otherwise they will return the same value every time. – SpYk3HH Jul 23 '13 at 15:09
  • Downvoting of this severity (-3 as I write) is inappropriate. The questioner certainly asks for a regex, but actually the use of modifier symbols for attribute = value was something I had not heard of and is appropriate for the question: just because the questioner asks for one thing doesn't mean you shouldn't tell them about an equally good/better/simpler way of doing what they want. – mike rodent Dec 09 '17 at 08:19