5

I would like to affect all my elements containing a class almost similar, i could iterate and use the iterator to do the job, but i am wondering if we could use a regex in this case:

$('.player').removeClass('player-1 player-2 player-3'); //player-n

Can the jQuery removeClass (and globaly the jquery methods which affects the DOM) treat a regex here ?

I am trying this:

$('.player').removeClass('/player-[0-9]+/');

The regex match i have tested it here, but it doesn't work on my DOM, does jQuery support the regex in this case?

Ludo
  • 5,060
  • 15
  • 53
  • 85

2 Answers2

2

You could use any css selector which are kindof a regex.

http://api.jquery.com/category/selectors/

in your case it would be

$("[class^='player-']")

or

$("[class|='player']")

mercsen
  • 692
  • 1
  • 5
  • 21
1

You can use filter like this

var elements = $('.player').filter(function() {
    return this.className = this.className.replace(/player-[0-9]+/, '');
});

Fiddle

Community
  • 1
  • 1
iConnor
  • 19,997
  • 14
  • 62
  • 97