Reputation: 22167
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
Upvotes: 2
Views: 565
Reputation: 5647
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;});
Upvotes: 3
Reputation: 191749
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/
Upvotes: 4