473,396 Members | 1,783 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,396 software developers and data experts.

JavaScript RegExp Quantifiers

I have the following script that I am using to test some JavaScript RegExp
code:

function RE()
{
var testing1=new RegExp("[.]*");
var testing2=new RegExp("[.]{0,}");
var testing3=new RegExp("[.]+");
var testing4=new RegExp("[.]{1,}");
window.alert(testing1.test("ab")+"\n"+testing2.tes t("ab")+"\n"+testing3.test("ab")+"\n"+testing4.tes t("ab"));}When this script is run, the window.alert contains the following results:truetruefalsefalseThe the first two trues make since, but why are the falses false? Thequantifiers in the RegExp, if I understand correctly, say it should beallowed to have 1 or more characters (other than newlines and lineterminators), and I know that 'a' and 'b' are not newlines or lineterminators, and "ab" is more than 1 character. I am using Internet Explorer6.0. Is this a bug in IE? Am I doing something else wrong? Any help would beappreciated. Thanks.--Nathan So****************@hotmail.comhttp:/...ansokalski.com/

Jun 27 '08 #1
2 1089
Nathan Sokalski wrote:
I have the following script that I am using to test some JavaScript RegExp
code:

function RE()
{
var testing1=new RegExp("[.]*");
var testing2=new RegExp("[.]{0,}");
var testing3=new RegExp("[.]+");
var testing4=new RegExp("[.]{1,}");
window.alert(testing1.test("ab")+"\n"+testing2.tes t("ab")+"\n"+testing3.test("ab")+"\n"+testing4.tes t("ab"));}When this script is run, the window.alert contains the following results:truetruefalsefalseThe the first two trues make since, but why are the falses false? Thequantifiers in the RegExp, if I understand correctly, say it should beallowed to have 1 or more characters (other than newlines and lineterminators), and I know that 'a' and 'b' are not newlines or lineterminators, and "ab" is more than 1 character. I am using Internet Explorer6.0. Is this a bug in IE? Am I doing something else wrong? Any help would beappreciated. Thanks.--Nathan So****************@hotmail.comhttp:/...ansokalski.com/
Inside a set, the period doesn't mean "any character except newline", it
just means a period.

testing3.test("ab.") will return true, as there is a period in the string.
The testing1 and testing2 patterns are equivalent, and also equivalent
with the pattern "\.*".

The testing3 and testing4 patterns are equivalent, and also equivalent
with the pattern "\.+".

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
Göran Andersson wrote on 11-6-2008 :
Nathan Sokalski wrote:
>I have the following script that I am using to test some JavaScript RegExp
code:

function RE()
{
var testing1=new RegExp("[.]*");
var testing2=new RegExp("[.]{0,}");
var testing3=new RegExp("[.]+");
var testing4=new RegExp("[.]{1,}");

window.alert(testing1.test("ab")+"\n"+testing2.te st("ab")+"\n"+testing3.test("ab")+"\n"+testing4.te st("ab"));}When
this script is run, the window.alert contains the following
results:truetruefalsefalseThe the first two trues make since, but why are
the falses false? Thequantifiers in the RegExp, if I understand correctly,
say it should beallowed to have 1 or more characters (other than newlines
and lineterminators), and I know that 'a' and 'b' are not newlines or
lineterminators, and "ab" is more than 1 character. I am using Internet
Explorer6.0. Is this a bug in IE? Am I doing something else wrong? Any help
would beappreciated. Thanks.--Nathan
So****************@hotmail.comhttp:/...ansokalski.com/

Inside a set, the period doesn't mean "any character except newline", it just
means a period.

testing3.test("ab.") will return true, as there is a period in the string.
The testing1 and testing2 patterns are equivalent, and also equivalent with
the pattern "\.*".

The testing3 and testing4 patterns are equivalent, and also equivalent with
the pattern "\.+".
And to add to this:
[.]* means: a period, *zero* or more times. And "ab" contains zero
periods.
[.]+ means: at least *one* period, so "ab" does not match.

Hans Kesting
Jun 27 '08 #3

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

Similar topics

3
by: Filip | last post by:
this is driving my crazy: $set = '<Error Type="string" Value="System Error" /><ErrorCode Type="int" Value="0" />'; preg_match('%<Error .*? Value="(.*)"%', $set, $matches); echo $matches; ...
7
by: Richard Trahan | last post by:
I need a javascript function to hex-encode a plus sign so I can pass the plus sign as an argument in a GET request. escape() and encodeURI() don't do it (and probably shouldn't, because '+' is a...
2
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to...
1
by: weston | last post by:
Has anyone ever encountered trouble with regular expressions not capturing matches specified by parentheses? I seem to have a weird situation where a regular expression is matching the String I'm...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
8
by: reflex | last post by:
Hi, i have script with pattern (href=?(.*)({1}+)?), where i match any occurence of url and replace hyperlink so i have only anchor in it. var regExp = /href=?(.*)({1}+)?/ig; var wholeContent...
7
by: Leif902 | last post by:
After much searching of google, the closest I can find is highlighting search terms... however, this is not what I wanted to do. Does anyone know how to parse through a specific element (lets say...
16
by: shyamg | last post by:
Hi, this is my javascript validating the fields in mozilla FF but its working and validating only one field. how to write the and how to works the script .................. function...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.