Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Isalpha, isalnum in Javascript?

7,454 views
Skip to first unread message

A.P.

unread,
Jan 2, 1998, 3:00:00 AM1/2/98
to

Hello,
I would like to know if there's a way to check if a character is
alphanumeric or if it belongs to a certain class of characters without
having to check explictly for al the cases with an endless if. I've
tried with character>='A' and also with the Ascii code but I had no
success.
Please answer to my mailbox too. Thank you very much in advance.


Thomas Salvador

unread,
Jan 4, 1998, 3:00:00 AM1/4/98
to

hi.

>having to check explictly for al the cases with an endless if. I've
>tried with character>='A' and also with the Ascii code but I had no
>success.

hmmm, this should work.

function isalpha(c) {
return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
}

function isdigit(c) {
return ((c >= '0') && (c <= '9'));
}

function isalnum(c) {
return (isalpha(c) || isdigit(c));
}

bye

--
thomas....@fernuni-hagen.de -+- http://www.come.to/salvador aka
http://www.stud.fernuni-hagen.de/q4307909/welcome.htm

Thogek

unread,
Jan 7, 1998, 3:00:00 AM1/7/98
to

thomas....@fernuni-hagen.de writes:
>>having to check explictly for al the cases with an endless if. I've
>>tried with character>='A' and also with the Ascii code but I had no
>>success.
>
>hmmm, this should work.
>
> function isalpha(c) {
> return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
> }
>
> function isdigit(c) {
> return ((c >= '0') && (c <= '9'));
> }
>
> function isalnum(c) {
> return (isalpha(c) || isdigit(c));
> }

Here's some funky-looking functions that'll also work:

var alphas = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var digits = "0123456789";

function isAlpha(c) {
return (alphas.indexOf(c) != -1);
}

function isDigit(c) {
return (digits.indexOf(c) != -1);
}

function isAlphaNum(c) {
return (isAlpha(c) || isDigit(c));
}

--
========================================================================
Thogek
tho...@alumni.caltech.edu
http://www.alumni.caltech.edu/~thogek

ms.20...@gmail.com

unread,
Jan 17, 2020, 3:50:14 AM1/17/20
to
On Friday, 2 January 1998 10:00:00 UTC+2, A.P. wrote:
> Hello,
> I would like to know if there's a way to check if a character is
> alphanumeric or if it belongs to a certain class of characters without
> having to check explicitly for all the cases with an endless if. I've
> tried with character>='A' and also with the Ascii code but I had no
> success.
> Please answer to my mailbox too. Thank you very much in advance.


You can use regex expressions to check, but it will check for English letters,
code:
var isAlpha = (c) => (/[A-Za-z]/).test(c);

JJ

unread,
Jan 17, 2020, 11:05:20 AM1/17/20
to
I taut I taw a millennial.

dr.j.r....@gmail.com

unread,
Jan 17, 2020, 4:07:35 PM1/17/20
to
On Friday, 17 January 2020 16:05:20 UTC, JJ wrote:
> On Fri, 17 Jan 2020 00:50:09 -0800 (PST), ms.2052001#gmail.com wrote:
> > On Friday, 2 January 1998 10:00:00 UTC+2, A.P. wrote:
> >> Hello,
> > ...
> > var isAlpha = (c) => (/[A-Za-z]/).test(c);
>
> I taut I taw a millennial.

Indeed. The answers given might have been reasonable in those far-off days, but appear inadequate now, except for those who believe that the only real characters are those on the keyboard of an American PC.

ISTM that Java has a Character entity, with methods such as Character.getType(), and that Java can access many sorts of information about characters which we here could write as \u0000 to \uFFFF - and higher.

So : what information can modern JavaScript obtain about Unicode characters such as the single characters `xi` and `XI` and strange characters existing in Eastern alphabets such as Danish, Russian, Japanese ... ?


--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |

Ben Bacarisse

unread,
Jan 17, 2020, 4:10:12 PM1/17/20
to
If you have ECMAScript 9, you can use

/\p{Letter}/u

as the pattern to match all Unicode characters with property
General_Category = Letter. Still not 100%, but it might be what's wanted.

--
Ben.

dr.j.r....@gmail.com

unread,
Jan 18, 2020, 5:42:29 AM1/18/20
to
On Friday, 17 January 2020 21:10:12 UTC, Ben Bacarisse wrote:

> If you have ECMAScript 9, you can use

Well, yes, but for Web authors the need must be for the readers to have ECMAscript 9.


> /\p{Letter}/u
>
> as the pattern to match all Unicode characters with property
> General_Category = Letter. Still not 100%, but it might be what's wanted.

My up-to-date Firefox 72.0.1 (64-bit) then appears not to have ECMAScript 9. Is there now a way in ECMAScript to determine the ECMAScript version number?

H'mm - if there is such a way, it would be necessary, I suppose, to use the above RegExp within try...catch, and compose a moderately graceful failure.

JJ

unread,
Jan 18, 2020, 9:01:33 AM1/18/20
to
On Sat, 18 Jan 2020 02:42:22 -0800 (PST), dr.j.r....@gmail.com wrote:
>
> My up-to-date Firefox 72.0.1 (64-bit) then appears not to have ECMAScript
> 9. Is there now a way in ECMAScript to determine the ECMAScript version
> number?

Probably not.

> H'mm - if there is such a way, it would be necessary, I suppose, to use
> the above RegExp within try...catch, and compose a moderately graceful
> failure.

It can be determined by checking feature presence and/or functionalities.

JJ

unread,
Jan 18, 2020, 9:01:33 AM1/18/20
to
On Fri, 17 Jan 2020 13:07:27 -0800 (PST), dr.j.r....@gmail.com wrote:
>
> So : what information can modern JavaScript obtain about Unicode
> characters such as the single characters `xi` and `XI` and strange
> characters existing in Eastern alphabets such as Danish, Russian,
> Japanese ... ?

Only the code point.

Thomas 'PointedEars' Lahn

unread,
Jan 18, 2020, 1:14:06 PM1/18/20
to
Much more after reading in the Unicode Character Database (BTDT in my JSX,
see UnicodeData.js, UnicodeData.txt, and jsx.regexp.RegExp in regexp.js):

<http://www.unicode.org/ucd/>

<https://stackoverflow.com/a/8568325/855543> (my answer in 2011)

--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Michael Haufe (TNO)

unread,
Jan 18, 2020, 3:13:07 PM1/18/20
to
On Saturday, January 18, 2020 at 4:42:29 AM UTC-6, dr.j.r...@gmail.com wrote:
> On Friday, 17 January 2020 21:10:12 UTC, Ben Bacarisse wrote:
>
> > If you have ECMAScript 9, you can use
>
> Well, yes, but for Web authors the need must be for the readers to have ECMAscript 9.


There is a babel plugin for this:

<https://babeljs.io/docs/en/next/babel-plugin-proposal-unicode-property-regex.html>

with an example:

<https://mothereff.in/regexpu#input=var+regex+%3D+/%5Cp%7BScript_Extensions%3DGreek%7D/u%3B&unicodePropertyEscape=1>

Julio Di Egidio

unread,
Jan 19, 2020, 1:07:34 PM1/19/20
to
On Friday, 17 January 2020 22:10:12 UTC+1, Ben Bacarisse wrote:
> ms.20...@gmail.com writes:
> > On Friday, 2 January 1998 10:00:00 UTC+2, A.P. wrote:
> >> Hello,
> >> I would like to know if there's a way to check if a character is
> >> alphanumeric or if it belongs to a certain class of characters without
> >> having to check explicitly for all the cases with an endless if. I've
> >> tried with character>='A' and also with the Ascii code but I had no
> >> success.
> >> Please answer to my mailbox too. Thank you very much in advance.
> >
> > You can use regex expressions to check, but it will check for English letters,
> > code:
> > var isAlpha = (c) => (/[A-Za-z]/).test(c);

You would use Unicode code points in character ranges, as in the last
example here:
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes>

> If you have ECMAScript 9, you can use
>
> /\p{Letter}/u

If all you have is a hammer...

> as the pattern to match all Unicode characters with property
> General_Category = Letter. Still not 100%, but it might be what's
> wanted.

LOL. Haven't you and the others noticed that you are replying to a thread
from 1998? As far as I remember, at the time JS didn't have RegExp at all,
and even a quite limited support (certainly browser-wise) for Unicode in
strings: which is the reason for that question...

Julio

dr.j.r....@gmail.com

unread,
Jan 19, 2020, 6:39:53 PM1/19/20
to
On Sunday, 19 January 2020 18:07:34 UTC, Julio Di Egidio wrote:


> LOL. Haven't you and the others noticed that you are replying to a thread
> from 1998?

One does not reply to a thread; one replies to an article. My first article in the thread was only technically a Reply; in essence, it proposed renewing the question.


> As far as I remember, at the time JS didn't have RegExp at all,
> and even a quite limited support (certainly browser-wise) for Unicode in
> strings: which is the reason for that question...

That may depend on which parts of 1998 you remember.

I am holding David Flanagan's JavaScript Pocket Reference, First Edition, October 1998. Its Object Reference part includes a RegExp section (Availability, Core JavaScript 1.2), and its description of the String Data Type says that ECMA-262 requires support of full 16-bit Unicode.

It does say that IE4 supports Unicode but Navigator 4 does not.

His Definitive Guide, 3rd Edn, says much the same, at greater length; the Preface is dated April 1998, publication June 1998.

Julio Di Egidio

unread,
Jan 19, 2020, 6:46:01 PM1/19/20
to
On Monday, 20 January 2020 00:39:53 UTC+1, dr.j.r...@gmail.com wrote:
> On Sunday, 19 January 2020 18:07:34 UTC, Julio Di Egidio wrote:
>
> > LOL. Haven't you and the others noticed that you are replying to a thread
> > from 1998?
>
> One does not reply to a thread; one replies to an article. My first article
> in the thread was only technically a Reply; in essence, it proposed renewing
> the question.

Besides that it's not an article but just a technical question, I hadn't
even mentioned it but it is actually VERY inappropriate to resurrect years
old threads, you guys still can't get your head around the fact that this
is not a forum and you are rather messing up with the Usenet ARCHIVE.

I mean, I am even being charitable, you guys have no fucking clue...

HTH and EOD.

Julio
0 new messages