Click to See Complete Forum and Search --> : Reg Expression for any printable characters
oldflint
04-08-2003, 11:47 AM
Apparently got stuck with the simple task to validate an entry with any PRINTABLE characters using search method. Something like:
if (str.search( . . . ) != -1)
return true;
else
return false;
Any idea would be really appreciated!!!
viravan
04-08-2003, 02:21 PM
If what you meant by a printable character an ASCII character with code in the range of 31 to 126, you can simply loop thru the string and use the charCodeAt function to check and see if the character code is inside or outside of the range.
:)
V.V.
Nedals
04-08-2003, 02:35 PM
Is this what you want?
if (str.search( /\w/) != -1)
where \w is any SINGLE alpha-numeric character
\w+ is one or more
and /^\w+$/ starts at the beginning '^' of 'str', checks for one or more alphanumeric chars ONLY, to the end '$' of 'str'
oldflint
04-08-2003, 02:36 PM
Originally posted by viravan
If what you meant by a printable character an ASCII character with code in the range of 31 to 126, you can simply loop thru the string and use the charCodeAt function to check and see if the character code is inside or outside of the range.
That's exactly what I mean, and I tried similar what you suggested. But apparently it will be too slow for making almost 100 comparisons with the user's entry, say if it's a posting of half page text like on this forum.
I was thinking more like using Predefined Sets, i.e. [:print:]
or [:graph:]. but nobody really gives any examples of syntax with those things.
Thnx!
I didn't put that stupid EmotIcon up there. the combination of chars made it up. I meant "print" there!
Nedals
04-08-2003, 02:45 PM
Quickly cross posting.. lol
Do you mean 'print' or '[:print:]' ?
if (str.test( /print/)) {
I suggest you use 'test' instead. This should deal with 'pint'.
pryro told me yesterday.. Disable smilies, when you post, to get rid of them. (Below the edit box)
viravan
04-08-2003, 02:49 PM
But apparently it will be too slow for making almost 100 comparisons
So.... you think using RegExp will be faster, eh?
Good luck!
:)
V.V.
oldflint
04-08-2003, 02:49 PM
Originally posted by nedals
Is this what you want?
if (str.search( /\w/) != -1)
where \w is any SINGLE alpha-numeric character
\w+ is one or more
and /^\w+$/ starts at the beginning '^' of 'str', checks for one or more alphanumeric chars ONLY, to the end '$' of 'str'
To nedals,
Thnx for the reply. But I mean "all printable", not only alphanumeric... Including all this crap !@#$%^&*()_+, etc. all ASCII between 32 and 126.
Thnx again!
Nedals
04-08-2003, 07:13 PM
Try this...
if (str.search(/[ !-~]/) != -1) { alert('true') } else { alert('false') }
This will check for all chars between ASCII 32 and 126. The initial space, in the square brackets, is meant to be there.
jeffmott
04-08-2003, 08:30 PM
The initial space, in the square brackets, is meant to be thereThen why add the exclamation point? "!" is within the range of space through ~. You also ignored your own recommendation of using test instead of search, which is really what we want since we don't care where the character occurs; only if it does or not.if (/[^ -~]/.test(str))
alert("Non-printable found");
else
alert("All printable");
Nedals
04-08-2003, 09:00 PM
Then why add the exclamation point? "!" is within the range of space through ~.One of my many habbits :)
[ -~] just feels strange.
You also ignored your own recommendation of using test instead of search, which is really what we want since we don't care where the character occurs; only if it does or not. I agree, but not being sure what is actually being done, I simply left it as posted.
Quick one for you. Why the negation? [^ -~]
jeffmott
04-08-2003, 09:18 PM
Quick one for you. Why the negation? [^ -~]This way it is matching at least one non printable character for the result to be true. Otherwise you'd be matching at least one printable, which means that a string with no non-printables would return the same value as a string partially composed of non-printables because they both have at least one printable.
oldflint
04-09-2003, 09:15 AM
Thank you gentlemen!