Unfortunately all other answers are wrong, managed the wildcard/wildcard behavior, but did not understand that the behavior has to be as is the selector .classe
, just as it is in CSS2, CSS2.1 and CSS2.2
Both the selector .classe
how much the attribute selector [atributo~=valor]
do not behave like normal attribute selectors, I explained and stressed that well in the question, at this point:
Note: the selector per point (.
) not only takes attributes such as <div class="foo">
, he takes too <div class="alpha foo betha">
, i.e., it takes the attribute divided by "space". More details on http://www.w3.org/TR/css3-selectors/#class-html
Aliais follows the basic documentation link for those who are learning CSS and want to understand the differences (or confirm what I say):
The solution
To make a selector that behaves like the class selector (or this selector [atributo~=valor]
) of CSS with character wildcard similar to the %
of LIKE
in Mysql
The solution I arrived at will behave as .class
or how , was so:
//Seletor customizado
jQuery.expr[':']['class-wildcard'] = function (elem, index, match) {
//Separa as partes da string
var parse = $.trim(match[3]).split('?');
//Escapa os caracteres necessários
for (var i = 0, j = parse.length; i < j; ++i) {
parse[i] = parse[i].replace(/[-\][*+?)(:\\]/g, '\\$&');
}
// Gera a regex em formato de string
var cls = '\\b' + parse.join('\\S+') + '\\b';
return new RegExp(cls).test(elem.className);
};
console.log(":class-wildcard(js-use-?) -> ", $(":class-wildcard(js-use-?)").length);
console.log(":class-wildcard(foo?bar) -> ", $(":class-wildcard(foo?bar)").length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- abaixo será com o seletor :class-wildcard(js-use-?) -->
<div class="foo js-use-1"></div> <!-- será pego -->
<div class="js-use-2 bar"></div> <!-- será pego -->
<div class="baz js-use-3 boo"></div> <!-- será pego -->
<div class="js-use-empty"></div> <!-- será pego -->
<div class="foo wjs-use-1"></div> <!-- NÃO será pego -->
<div class="xjs-use-2 bar"></div> <!-- NÃO será pego -->
<div class="baz yjs-use-3 boo"></div> <!-- NÃO será pego -->
<div class="zjs-use-empty"></div> <!-- NÃO será pego -->
<!-- abaixo será com o seletor :class-wildcard(foo?bar) -->
<div class="foobazbar"></div> <!-- será pego -->
<div class="fooxyzbar"></div> <!-- será pego -->
<div class="fooabcbar"></div> <!-- será pego -->
<div class="col1 foobazbar"></div> <!-- será pego -->
<div class="col2 fooxyzbar custom"></div> <!-- será pego -->
<div class="fooabcbar col3"></div> <!-- será pego -->
<div class="fooar col4"></div> <!-- NÃO será pego -->
Explaining the code
To better explain the code, parse splits strings into arrays, thus:
js-use-?
flipped ["js-use", ""]
foo?bar
flipped ["foo", "bar"]
This way it is possible to apply the "escape" in characters that could affect the regex which occurs within the for()
:
parse[i] = parse[i].replace(/[-\][*+?)(:\\]/g, '\\$&');
Then he joins with .join
the arrays again using as separator the \S+
which will be the wildcard/wildcard in the case, the expression \S
("s" in capital letters) is the negation of \s
, ie the joker can be anything but space, tab or line break.
Note that it was also used \b
at the ends, such an expression:
\bfoobar\b
Seria "similar" to this:
(^|\s)foobar($|\s)
That is, the value tested in regex must start and end with the word of the expression, or must contain spaces.
Then after the join
the parameters would have results like this:
js-use-?
turns this regex /\bjs-use-\S+\b/
foo?bar
turns this regex /\bfoo\S+bar\b/
a?b?cde?xyz?
turns this regex /\ba\S+b\S+cde\S+xyz\S+\b/
Officially jQuery supports CSS3 selectors, which are the ones you already know. Unfortunately there are no wildcards (wildcards) in these selectors, so this feature is not officially supported. However, I found this extension here that can serve you, it uses Regex (Regular Expressions): http://james.padolsey.com/javascript/regex-selector-for-jquery/.
– Joel Rodrigues
Check out this answer: http://stackoverflow.com/a/190255/1161005
– user622