extreme64
6/20/2019 - 7:34 AM

Selecting in CSS

/** regex CSS */

//Regex Selector for jQuery
//28 Jan 2009

//A while ago I published an article explaining the utter awesomeness of extending jQuery’s filter selectors. 
//Building on that here’s something new; a regular expression selector. jQuery’s current attribute selectors (CSS3) 
//do allow basic regex notation but no where near as much as this:
//:regex
// https://j11y.io/javascript/regex-selector-for-jquery/

jQuery.expr[':'].regex = function(elem, index, match) {
	var matchParams = match[3].split(','),
		validLabels = /^(data|css):/,
		attr = {
			method: matchParams[0].match(validLabels) ?
									matchParams[0].split(':')[0] : 'attr',
			property: matchParams.shift().replace(validLabels,'')
		},
		regexFlags = 'ig',
		regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
	return regex.test(jQuery(elem)[attr.method](attr.property));
}


$('p:regex(class,[A-z])').css('color', 'red')