View Full Version : Help!!! RegExp for any printable characters
04-08-2003, 09:01 PM
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:
if (str.search( . . . ) != -1)
I was thinking more like using Predefined Sets, i.e. [print:]
or [:graph:]. but nobody really gives any examples of syntax with those things.
Any idea would be greatly appreciated!!!
04-08-2003, 10:47 PM
How about the whole string method for ya?
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 ) )
04-08-2003, 11:00 PM
} Would be much more efficient than what Beetle wrote.
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)
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:
var re=/[^ -~]/;
(Your last code wouldn't - you check for a negated charset followed by end of string.)
04-08-2003, 11:12 PM
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.?
04-08-2003, 11:13 PM
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.
04-08-2003, 11:32 PM
Thank you again, gentlemen!
04-09-2003, 02:17 PM
You have no chance make your time!
(sorry... I'll get my coat on the way out...)
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.