Apparently got stuck with the simple task to validate an entry with any PRINTABLE characters (ASCII between 32 and 126) using search (or any other) method. Something like:
String.prototype.isPrintable = function()
{
var pat = '';
for ( var i = 32; i <= 126; i++ )
{
pat += "\\x" + i.toString(16);
}
var regex = new RegExp( "[^" + pat + "]" );
return ( !regex.test( this ) )
}
Is it better to test the entire string for compliancy, or let the regex look at the compliancy of each char? (if you follow me, using a negated character set)
Code:
String.prototype.isPrintable=function(){
var re=/[^\x20-\x7e]$/;
return !re.test(this);
}
Well, actually I realised that your code would work in any case, since you would have returned true only if no character without the range existed in it. I just used my start and end from my habit of making sure the whole string is checked. This would actually be more efficient:
Code:
String.prototype.isPrintable=function(){
var re=/[^ -~]/;
return !re.test(this);
}
(Your last code wouldn't - you check for a negated charset followed by end of string.)
But say, I gotta take care about a different locale. Not only Latin alphabet. does anybody know how to use those predefined sets, i.e. [:print:] or [:graph:], etc.?