Skip to content

Instantly share code, notes, and snippets.

@naturalkei
Forked from spyesx/jquery.hasClassRegEx.js
Created March 2, 2016 05:03
Show Gist options
  • Save naturalkei/df51898cfacc0db2d8d1 to your computer and use it in GitHub Desktop.
Save naturalkei/df51898cfacc0db2d8d1 to your computer and use it in GitHub Desktop.
jQuery.hasClassRegEx() hasClass by using a Regex
(function($)
{
$.fn.hasClassRegEx = function(regex)
{
var classes = $(this).attr('class');
if(!classes || !regex){ return false; }
classes = classes.split(' ');
var len = classes.length;
for(var i=0; i<len; i++)
{
if(classes[i].match(regex)){ return true; }
}
return false;
};
})(jQuery);
/*
<span id="hasClassRegEx" class="Test Testing someTest aaTestaa"></span>
$("#hasClassRegEx").hasClassRegEx(); // false
$("#hasClassRegEx").hasClassRegEx(''); // false
$("#hasClassRegEx").hasClassRegEx('Test'); // true
$("#hasClassRegEx").hasClassRegEx(/ /); // false
$("#hasClassRegEx").hasClassRegEx(/Test/); // true
$("#hasClassRegEx").hasClassRegEx(/^Test/); // true
$("#hasClassRegEx").hasClassRegEx(/Test$/); // true
$("#hasClassRegEx").hasClassRegEx(/^Test$/); // true
$("#hasClassRegEx").hasClassRegEx(/test/); // false
$("#hasClassRegEx").hasClassRegEx(/test/i); // true
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment