String.prototype.isPrintable=function(){
var re=/^[\x20-\x7e]*$/;
return re.test(this);
} Would be much more efficient than what Beetle wrote.

beetle

04-08-2003, 11:01 PM

Ooh, ranges with ascii chars. Missed that one.

Nice liorean :thumbsup:

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)
String.prototype.isPrintable=function(){
var re=/[^\x20-\x7e]$/;
return !re.test(this);
}

liorean

04-08-2003, 11:11 PM

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:
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.)

oldflint

04-08-2003, 11:12 PM

Wow!

Thank you gentlemen big time!

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.?

THANKS AGAIN!!!

liorean

04-08-2003, 11:13 PM

JavaScript doesn't handle the predefined sets, just a subset of PCRE. You'll have to specify the ranges for the characters in question.

beetle

04-08-2003, 11:13 PM

Originally posted by liorean
(Your last code wouldn't - you check for a negated charset followed by end of string.) Oops, that was a typo.