2

I done now but i have to call 2 times .removeClass() functions

How can I merge this to find the result in one regex call?

HTML :

<div class="one-abc two-cba three-bac"></div>

jQuery :

$('div')
  .removeClass(function(i,c){var m=c.match(/one-[a-z]{0,5}/);return m?m[0]:m;})
  .removeClass(function(i,c){var m=c.match(/two-[a-z]{0,5}/);return m?m[0]:m;});

Demo : http://jsbin.com/umoded/1/edit


Find contains class name in multiple classes

Remove matching class with regex

Community
  • 1
  • 1
l2aelba
  • 21,591
  • 22
  • 102
  • 138
  • 1
    I'm guessing this question assumes knowledge of another question... If that's the case please give a link pointing there so it is easier for future readers to follow what you want to do! – Loupax Feb 15 '13 at 13:15

2 Answers2

4

Combine both expressions in one, use g, and join the matches:

  var m = c.match(/(one-[a-z]{0,5}|two-[a-z]{0,5})/g);
  return m ? m.join(' ') : m;

http://jsfiddle.net/ExplosionPIlls/mnYpB/

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
3

from api.jquery.com:

More than one class may be removed at a time, separated by a space, from the set of matched elements, like so:
$('p').removeClass('myClass yourClass')

-->

.removeClass(function(i,c){
    var m=c.match(/one-[a-z]{0,5}/);
    var n=c.match(/two-[a-z]{0,5}/);
    return m+" "+n;});
Vogel612
  • 5,620
  • 5
  • 48
  • 73