473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Strange RegExp problem

r
Hello,

It seems delimiters can cause trouble sometimes.

Look at this :

<script type="text/javascript">
function isDigit(s) {
var DECIMAL = '\\.';
var exp = '/(^[+]?0(' + DECIMAL
+'0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)' + DECIMAL
+'(0*[1-9][0-9]*)))$)|(^[-+]?[1-9]+[0-9]*' + DECIMAL +'0+$)/';
return RegExp(exp).test(s);
};
document.write(isDigit('123.45') ? '1' : '0');
document.write(isDigit('123') ? '1' : '0');
document.write(isDigit('123.00') ? '1' : '0');
</script>

With / delimiters, third test fails.
With | delimiters, it's ok.

Same behavior with firefox,ie7,safari.

Can someone explain this ?

Thanks.
Oct 14 '08 #1
4 2516
r@ph wrote:
var exp = '/(^[+]?0(' + DECIMAL
+'0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)' + DECIMAL
+'(0*[1-9][0-9]*)))$)|(^[-+]?[1-9]+[0-9]*' + DECIMAL +'0+$)/';
return RegExp(exp).test(s);
// serve as delimiters for regular expression literals. You don't use
them in strings passed to the RegExp constructor, unless you really want
to match a slash.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 14 '08 #2
r
> var exp = '/(^[+]?0(' + DECIMAL
>+'0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)' + DECIMAL
+'(0*[1-9][0-9]*)))$)|(^[-+]?[1-9]+[0-9]*' + DECIMAL +'0+$)/';
return RegExp(exp).test(s);
// serve as delimiters for regular expression literals. You don't use them
in strings passed to the RegExp constructor, unless you really want to
match a slash.

Hi Martin,
Can you explain this a little more please ?
In my sample, why only the 3rd case fails ?
Thanks a lot.
Oct 14 '08 #3
"r@ph" <r-*****************@inrezo.comwrites:
It seems delimiters can cause trouble sometimes.
No doubt. Most things can :)
Look at this :

<script type="text/javascript">
function isDigit(s) {
var DECIMAL = '\\.';
var exp = '/(^[+]?0(' + DECIMAL
+'0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)' + DECIMAL
+'(0*[1-9][0-9]*)))$)|(^[-+]?[1-9]+[0-9]*' + DECIMAL +'0+$)/';
ICK. Now I have looked. I need to go wash my eyes. With soap.

Ahem, anyway ...
The "/" at the start and end of "exp" is pretty certainly wrong.

A javascript regexp *literal* is delimited by slashes, e.g.,
/foo|bar/. The corresponding constructor call would be RegExp("foo|bar"),
where the regexp is in a string literal, delimited by the string quotes,
and therefore doesn't need any other delimination ... deliminion ...
something.

The "/"'s above merely cause the first and last alternative of the
regexp to fail because they expect a slash before the start of the
string, or after the end.
return RegExp(exp).test(s);
};
document.write(isDigit('123.45') ? '1' : '0');
document.write(isDigit('123') ? '1' : '0');
document.write(isDigit('123.00') ? '1' : '0');
</script>

With / delimiters, third test fails.
With | delimiters, it's ok.
Where did you put the '|' "delimiters"? In the same palce as the '/'?
Then the first and last alternative would start to work, and you would
also have added two ways of matching the empty substring, meaning that
*all* strings would match. Try isDigit("Arglebargle").
Same behavior with firefox,ie7,safari.
Then it's probably intended behavior.
Can someone explain this ?
Yep, it's what you asked for :)

Stick to regexp literals if possible, they are easier to read,, and
you don't have to escape your backslashes.
var exp = '/(^[+]?0(' + DECIMAL
+'0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)' + DECIMAL
+'(0*[1-9][0-9]*)))$)|(^[-+]?[1-9]+[0-9]*' + DECIMAL +'0+$)/';
If I read the regexp correctly, it should match any of:
- An optional plus and a single zero, optionally followed by a decimal
point and one or more zeros.
- An optional sign and a non-zero digit followed by any number of digits.
- An optional sign and either zero or (a non-zero digit followed by any
number of digits), followed by a decimal point and a sequence of digits
containing at least one non-zero digit.
- An optional sign and a non-zero digit followed by any number of digits,
followed by a decimal point and a nonzero number of zeros.

I.e., an optional sign, either a single zero multiple digits not startine
with a zero, and optionally a decimal point and more than one digit -
except you don't allow minus zero.

It might be easier to use two checks:

/^[-+]?(?:0|[1-9]\d+)(?:\.\d+)?$/
AND NOT
/^-0(?:\.0+)$/

If that's not possible, I think the above can also be made a little
shorter (and avoid the DECIMAL constant, unless you do it for
readablilty):
/^(?:\+?0(?:\.0+)?|[-+]?(?:0\.0*[1-9]\d*|[1-9]\d*(?:\.\d+)?))$/

Best of luck.
/L 'likes regexps'
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Oct 14 '08 #4
r
Hello,

Thanks for this complete answer.
A javascript regexp *literal* is delimited by slashes, e.g.,
/foo|bar/. The corresponding constructor call would be RegExp("foo|bar"),
where the regexp is in a string literal, delimited by the string quotes,
and therefore doesn't need any other delimination ... deliminion ...
something.
Understood.
Where did you put the '|' "delimiters"? In the same palce as the '/'?
Then the first and last alternative would start to work, and you would
also have added two ways of matching the empty substring, meaning that
*all* strings would match. Try isDigit("Arglebargle").
Exact. I'm confused :S
This code and my problem come from the tablesorter jquery plugin. I warn the
author now.

Thanks again.
Raph
Oct 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Magnus Lie Hetland | last post by:
I'm working on a project (Atox) where I need to match quite a few regular expressions (several hundred) in reasonably large text files. I've found that this can easily get rather slow. (There are...
0
by: Ed Leafe | last post by:
I recently upgraded to 4.1 alpha (MySQL 4.1.0-alpha-standard-log) on my Linux server, and came across a problem with a query that had been working in 3.23 that no longer worked in 4.1a. I've...
10
by: Andrew DeFaria | last post by:
I was reading my O'Reilly JavaScript The Definitive Guide when I came across RegExp and thought I could tighten up my JavaScript code that checks for a valid email address. Why does the following...
6
by: Mark Findlay | last post by:
I am trying to figure out how to set up my reg exp search so that the search will only match on the exact word. Here is the current problem code: Word1 = "RealPlayer.exe" Word2 = "Player.exe"...
1
by: geos | last post by:
hello, I have the problem writing the regular expression to verify the valid system path in the way that RegExp.$1 has to contain path up to the parent folder of a file, and RegExp.$2 should...
6
by: Christoph | last post by:
I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters. I'm using the following code but it doesn't seem to be...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
9
by: vbfoobar | last post by:
Hello I am looking for python code that takes as input a list of strings (most similar, but not necessarily, and rather short: say not longer than 50 chars) and that computes and outputs the...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.