JavaScript Regex

G

Gene Wirchenko

Dear JavaScripters:

I have not found quite the detail that I need for regexes. If
you have a URL to a good site, please post it. In particular, how do
I group subexpressions?

I wish to parse a string to see if it is a valid fixed-decimal
value.

I start with
/^

How do I check a set of characters optionally? I want to check
for a sign character. The string might start with "+" or "-", but if
it starts with neither, that is fine. Do I leave an empty choice in a
parens set? Is it then
([+-]|)
or
[+-]?
?

Then come optional digits:
\d*

Then another complex maybe for the decimal point and digits
following:
(\.\d*|)
or
(\.\d*)?
?

And the end
$/

The regex should accept:
123
+123
-123
123.
+123.
-123.
123.45
+123.45
-123.45

It will also accept
 
R

Ross McKay

I have not found quite the detail that I need for regexes. If
you have a URL to a good site, please post it.

http://www.regular-expressions.info/

But note:
http://www.regular-expressions.info/javascript.html

Also useful:
https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp
In particular, how do
I group subexpressions?

I wish to parse a string to see if it is a valid fixed-decimal
value.

I start with
/^

How do I check a set of characters optionally? I want to check
for a sign character. The string might start with "+" or "-", but if
it starts with neither, that is fine. Do I leave an empty choice in a
parens set? Is it then
([+-]|)
or
[+-]?
?

The latter.
Then come optional digits:
\d*

Then another complex maybe for the decimal point and digits
following:
(\.\d*|)
or
(\.\d*)?
?

Again, the latter.
[...]
The regex should accept:
123
+123
-123
123.
+123.
-123.
123.45
+123.45
-123.45

It will also accept
.
+.
-.
That is acceptable. I will check for digits with another regex.

See here for a good tutorial on this problem:
http://www.regular-expressions.info/floatingpoint.html
 
T

Thomas 'PointedEars' Lahn

Denis said:
I wish to parse a string to see if it is a valid fixed-decimal
value.

/^[+-]?\d{1,}(\.\d*)?$/

http://www.sined.co.uk/tmp/regex.htm

Wrong, see the ECMAScript Language Specification and, e. g., the JSON
website. And what is *fixed* about this?

!isNaN(+s)

where `s' is a String value, suffices for strings representing floating-
point values. If "fixed" is supposed to mean "a fixed number of decimals",
a more elaborate expression needs to be used, but not this one (what about
leading "." and scientific notation?). We have been over this.


PointedEars
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 12 jan 2012 in comp.lang.javascript:
Denis said:
I wish to parse a string to see if it is a valid fixed-decimal
value.

/^[+-]?\d{1,}(\.\d*)?$/

http://www.sined.co.uk/tmp/regex.htm

Wrong, see the ECMAScript Language Specification and, e. g., the JSON
website. And what is *fixed* about this?

!isNaN(+s)

Will also accept "2.2e7" which is not a fixed-decimal but floating.

There is no need for the +, as isNaN() will accept a string,
and do the conversion internally.
 
G

Gene Wirchenko


Thank you for the links and for the confirmation of my regex.

Actually, I am not concerned about floating-point. My app will
only use integers and fixed-point numbers.

Sincerely,

Gene Wirchenko
 
E

Evertjan.

Gene Wirchenko wrote on 12 jan 2012 in comp.lang.javascript:
Actually, I am not concerned about floating-point. My app will
only use integers and fixed-point numbers.

If so, and if you don't need to disallow whitespace,

var boolResult = !isNaN(str);

will do.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Gene Wirchenko wrote on 12 jan 2012 in comp.lang.javascript:


If so, and if you don't need to disallow whitespace,

var boolResult = !isNaN(str);

Accepts str = "0x0" which is probably not wanted.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Denis McMahon wrote on 12 jan 2012 in comp.lang.javascript:
I wish to parse a string to see if it is a valid fixed-decimal
value.

/^[+-]?\d{1,}(\.\d*)?$/

var result = !isNaN(t) && !/[e\s]/i.test(t)

[allows .123 ]

Such should NOT be allowed, if there is any option (IUPAP/SUNAMCO) - an
inadequately-guarded dot can too easily fail to be noticed.
 
D

Dr J R Stockton

In comp.lang.javascript message <8kosg7tha6n6epo47krtaulm9a269kvclo@4ax.
I have not found quite the detail that I need for regexes. If
you have a URL to a good site, please post it.

<http://www.merlyn.demon.co.uk/js-valid.htm> lists some that I have
found.

I wish to parse a string to see if it is a valid fixed-decimal
value.

Cannot actually be done. "2012" may in truth be 2^8+1, to base 5.

And 014747 is really a bit pattern representation of a computer
instruction, highly amusing to those who can read PDP-11 code; it does
not mean decimal 6631.

For human input, you should generally allow leading and trailing
whitespace. Then an optional sign. Then at least one digit. Then,
optionally, (a decimal point followed by at least one digit). In any
particular application, you can probably also limit the number of
digits; for example, in currency, except for Government work, any more
than say nine digits before the decimal point probably means that the
cat has walked along the keyboard, or the euro has crashed, etc.


<http://www.merlyn.demon.co.uk/js-valid.htm#VNP> has what you need, if
you remove the exponent part.
 
E

Evertjan.

Dr J R Stockton wrote on 14 jan 2012 in comp.lang.javascript:
n comp.lang.javascript message <[email protected]>


Accepts str = "0x0" which is probably not wanted.

Probably not, the OP stipulated that floating point etc. need not be
concidered, that's why I simplified my original.

Try for completeness:

var boolResult = !isNaN(t) && /[^\d\.]/i.test(t)
 
G

Gene Wirchenko

In comp.lang.javascript message <8kosg7tha6n6epo47krtaulm9a269kvclo@4ax.


<http://www.merlyn.demon.co.uk/js-valid.htm> lists some that I have
found.



Cannot actually be done. "2012" may in truth be 2^8+1, to base 5.

How fatuous and condescending! I am checking whether a string is
valid fixed-decimal value. Whether it can also be interpreted as
something else is irrelevant.

The rest of your post is similar. I *have* considered what I
require. What I asked about is but a small part of it. (I do believe
in doing my own work as much as I can.)

[snip]

Sincerely,

Gene Wirchenko
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Dr J R Stockton wrote on 14 jan 2012 in comp.lang.javascript:
n comp.lang.javascript message <[email protected]>


Accepts str = "0x0" which is probably not wanted.

Probably not, the OP stipulated that floating point etc. need not be
concidered, that's why I simplified my original.

Try for completeness:

var boolResult = !isNaN(t) && /[^\d\.]/i.test(t)

Works better with another ! !!

That will accept the deprecated forms '1.' and '.1'.

That will accept numbers so long that they read as Infinity.

I wonder whether Gene knows how to tell
whether a Number is positive or negative?
 
D

Dr J R Stockton

In comp.lang.javascript message <c4c7h7t2qghi49i0esm44sl00ki2cq27q9@4ax.
How fatuous and condescending! I am checking whether a string is
valid fixed-decimal value.

No, you are checking whether it can be interpreted as a valid integer or
fixed-point decimal value. One cannot tell what a string IS by mere
inspection of the string.
Whether it can also be interpreted as
something else is irrelevant.

The rest of your post is similar. I *have* considered what I
require. What I asked about is but a small part of it. (I do believe
in doing my own work as much as I can.)

Doing your own work where reasonable is good. But regurgitating all the
old and well-known follies here is not good. We on the whole, or I at
least, would prefer you to take advantage of what is already well-known
and sufficiently published in the field, and then to present new and
therefore interesting problems, so encouraging the advancement of
learning in general.

Remember what Newton said about the shoulders of giants. And note that
he probably did not invent that saying, but more likely got it
indirectly from Bernard of Chartres.
 
S

Scott Sauyet

Dr said:
Gene Wirchenko posted:

No, you are checking whether it can be interpreted as a valid integer or
fixed-point decimal value.  One cannot tell what a string IS by mere
inspection of the string.

Did you really misunderstand? Or are you being pedantic for no good
reason?

I think it was clear to all other participants approximately what Gene
was asking about, and several helpful responses have already been
given. Maybe there would have been slightly less ambiguity had he
said, "I wish to programatically determine if a given string matches a
standard format used to represent fixed-point values," or some such,
but I for one had no problem translating "I wish to parse a string to
see if it is a valid fixed-decimal value" to mean much the same.

Now, on the other hand, Gene, the first result in

http://www.google.com/search?q=javascript+regex+floating+point

is to a Stack Overflow page which has this answer:

/^[-+]?[0-9]*\.?[0-9]+$/

That might or might not meet your requirements (it allows leading
zeroes and bare decimals), but it could at least be a good starting
point. And it wasn't hard to find.

-- Scott
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Denis McMahon wrote on 12 jan 2012 in comp.lang.javascript:
I wish to parse a string to see if it is a valid fixed-decimal
value.

/^[+-]?\d{1,}(\.\d*)?$/

var result = !isNaN(t) && !/[e\s]/i.test(t)

[allows .123 ]

It also allows "Infinity" with optional sign.

Set t = "NaN", and one can see that "NaN" is wrong; it should be spelt
"Nan", because ECMAscript NaN is undoubtedly a Number but not a number.
 
D

Dr J R Stockton

In comp.lang.javascript message <fcf854ac-2bf2-45c9-b458-28f5b07b2c12@i6
g2000vbk.googlegroups.com>, Wed, 18 Jan 2012 05:42:13, Scott Sauyet
Did you really misunderstand? Or are you being pedantic for no good
reason?

I know more or less what he meant, but it was expressed in a woolly-
minded manner. Those are not exact descriptions of what he wants to
test for.

Before designing such a test, it is necessary to know what characters,
if any, can be present before or after the number; whether a sign is
permissible or compulsory or neither, whether a fixed-point number is
required to have a point in it, whether there must be a digit on each
side of the point or it is allowable to have a digit on only one side,
whether starting with <zero><digit> is allowed, whether there should be
any bound on the magnitude, whether there may be thousands separators
(or ones for lakhs & crores, to support the Indian market), etc.

AFAIK, the Canadian Government of Canada (or of Quebec) may impose a
requirement to accept also comma as the decimal separator.

Those who express their needs fully, clearly, and unambiguously may well
realise the solution while doing so, and otherwise are likely to get
useful, accurate answers.

Now, on the other hand, Gene, the first result in

http://www.google.com/search?q=javascript+regex+floating+point

is to a Stack Overflow page which has this answer:

/^[-+]?[0-9]*\.?[0-9]+$/

That might or might not meet your requirements (it allows leading
zeroes and bare decimals), but it could at least be a good starting
point. And it wasn't hard to find.

The answers on my page would have been easy to find, at least after I
gave the URL. And I know about \d.
 
E

Evertjan.

Dr J R Stockton wrote on 19 jan 2012 in comp.lang.javascript:
var result = !isNaN(t) && !/[e\s]/i.test(t)

[allows .123 ]

It also allows "Infinity" with optional sign.

This better?

Remember we are talking aboutr strings:
t = '-Infinity'
not
t = -Infinity

<script type='text/javascript'>

var t = '-Infinity';
//var t = 'NaN';
//var t = '.259';

var result = !isNaN(t) && !/[a-z\s]/i.test(t);

alert(t+' is a fixed point number?\n'+result);

Set t = "NaN", and one can see that "NaN" is wrong; it should be spelt
"Nan", because ECMAscript NaN is undoubtedly a Number but not a number.

A fixed point Number?
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Dr J R Stockton wrote on 19 jan 2012 in comp.lang.javascript:

A fixed point Number?

That had no especial reference to GW's number-strings. Variables of
type Number are invariably [supposed to be] IEEE 754 double-precision
binary floating-point format internally, as [the representations that I
have seen of] their internal formats are all in terms of sign, exponent,
and mantissa.

Although typeof NaN is "number", ECMA is clear that the type is
really 'Number' - but the value of NaN does not lie in the inclusive
range from -Infinity to +Infinity.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top