I want to check the 'type' of a string. Particularly, how do I distinguish jQuery selector strings from other strings? In other words, how should selectorTest be implemented in the following code?
var stringType = function( value ) {
var htmlExpr = /^[^<]*(<[\w\W]+>)[^>]*$/;
if ( htmlExpr.test(value) ) {
return "htmlstring";
}
if ( selectorTest ) {
return "selectorstring";
}
return "string";
}
maybe
($(value).size()>0
) ?It will test if the selector is recognized.
But in my opinion, it is a bit strange way to do...
You can do what jQuery does internally and check if it's HTML or not with the following regex:
e.g.:
Note that in more recent versions of jQuery, there's another check explicitly for "starting with
<
" and "ending with>
" to skip the regex (purely for speed). The check looks like this in core (as of jQuery 1.6.1):