-
-
Save naturalkei/df51898cfacc0db2d8d1 to your computer and use it in GitHub Desktop.
jQuery.hasClassRegEx() hasClass by using a Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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