By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

javascript regular expression methods, problem with escape characters

P: n/a
lev
Attempting to use javascript's regular expression methods to match a
particular lab number and building which would then be copied to a pair
of textfields, I found that general matching characters needing a
backslash were not recoginized. The following adapted code that finds
a two-button radio selection shows my problem:

function labstat(){
for (i=1; i<3; i++){
if(document.tester.rm_mod[i-1].checked){
var lb = document.tester.elements["lab"+(i)].value;//either
214 or 215*
var bld = document.tester.bldg.value;
if(/\*/.test(lb)){ //error:Undetermined comment
// if(/*/.test(lb)){ //error:Syntax error
// if(/5/.test(lb)){ //works to match only 215*
// if(/5\*/.test(lb)){ //true for all
// if(/\d\d\d/.test(lb)){ //false for all
document.tester.room1.value = lb;
document.tester.bldg1.value = bld;
}
}
}

It appears that the escaped characters are not recognized, whichever
form of the method is used, i.e var re = /\*/; // or = new
RegExp("\*");
if(re.test(lb)){
--------
With the --.match(__) method I have the same problem.

If someone sees my mistake of could direct me toward the source of this
problem, I would grateful.

Thanks

Sep 13 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a


lev wrote:

var lb = document.tester.elements["lab"+(i)].value;//either
214 or 215*
So a fitting regular expression is /^\d{3}\*?$/, the check would be

var testStrings = [ '214', '215*'];
var regularExpression = /^\d{3}\*?$/;
for (var i = 0, l = testStrings.length; i < l; i++) {
alert(regularExpression + ' matches ' + testStrings[i] + ': ' +
regularExpression.test(testStrings[i]));
}

and that works for me flawlessly with Firefox, IE, Opera.

If you get syntax errors then tell us which browser you are using and
try to reduce the code to a minimum but then present the exact code that
gives the error and not all variations you have tried.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 13 '06 #2

P: n/a
lev wrote:
Attempting to use javascript's regular expression methods to match a
particular lab number and building which would then be copied to a pair
of textfields, I found that general matching characters needing a
backslash were not recoginized. The following adapted code that finds
a two-button radio selection shows my problem:
[ snip code ]
There's something really wrong with your testing results. Are you
generating javascript output from another application or from a
database ? It might throw away backslashes in interpolated strings.

--
Bart

Sep 13 '06 #3

P: n/a
lev
Martin Honnen wrote:
So a fitting regular expression is /^\d{3}\*?$/, the check would be

var testStrings = [ '214', '215*'];
var regularExpression = /^\d{3}\*?$/;
for (var i = 0, l = testStrings.length; i < l; i++) {
alert(regularExpression + ' matches ' + testStrings[i] + ': ' +
regularExpression.test(testStrings[i]));
}

and that works for me flawlessly with Firefox, IE, Opera.

If you get syntax errors then tell us which browser you are using and
try to reduce the code to a minimum but then present the exact code that
gives the error and not all variations you have tried.


Martin Honnen
http://JavaScript.FAQTs.com/

Thank you for the response. However, using what you suggest,
triggering the script by the click of my radio key, results are the
same as before. General regular expressions are not recognized:

function labstat(){

var testStrings = [ '214', '215*'];
var regularExpression = /^\d{3}\*?$/;
for (var i = 0, l = testStrings.length; i < l; i++) {
alert(regularExpression + ' matches ' + testStrings[i] + ': ' +
regularExpression.test(testStrings[i]));
}

}

Error:"Unexpected quantifier"
This error is generated using the general expression /^\d{3}\*?$/.

Replacing regularExpression with the dummy expression /5/ does work
with a sequence of two alert boxes. The first reads "/5/ matches 214:
false" and the second reads "/5/ matches 215*: true" as expected.

Help,About Internet Explorer gives me:
Version: 6.0.2900.2180,xpsp_sp2_rtm.040803-2158
Cipher Strength: 128-bit
Product ID....
Update Versions:;SP2;
Based on NCSA Mosaic. NCSA Mosaic(TM); was developed at the National
Center for Supercomputing Applications at the University of Illinois at
Urbana-Champaign.
Distributed under a licensing agreement with Spyglass, Inc.
Contains security software licensed from RSA Data Security Inc.
Portions of this software are based in part on the work of the
Independent JPEG Group.
Multimedia software components, including Indeo(R); video, Indeo(R)
audio, and Web Design Effects are provided by Intel Corp...

Anything else to do to find the bug?

Thanks again,
lev

Sep 14 '06 #4

P: n/a
lev wrote:
[...]
function labstat(){

var testStrings = [ '214', '215*'];
var regularExpression = /^\d{3}\*?$/;
for (var i = 0, l = testStrings.length; i < l; i++) {
alert(regularExpression + ' matches ' + testStrings[i] + ': ' +
regularExpression.test(testStrings[i]));
}
}

Error:"Unexpected quantifier"
This error is generated using the general expression /^\d{3}\*?$/.
Well, I get the same error in MSIE with

/^d{3}*?$/

in stead of

/^\d{3}\*?$/

(so, without the backslashes). 'd' in stead of '\d' is no problem, but
repetition quantifiers like '*' can't refer to something like 'd{3}'.

Do you see those errors as well on http://dotinternet.be/temp/test.htm
? Is /^\d{3}\*?$/ the actual code that you see in browser view
source, or is it without the slashes there ? How do you generate the
javascript code ? In an .htm(l) page or via server script ?

--
Bart

Sep 14 '06 #5

P: n/a
lev

Bart Van der Donck wrote:
lev wrote:
[...]
function labstat(){

var testStrings = [ '214', '215*'];
var regularExpression = /^\d{3}\*?$/;
for (var i = 0, l = testStrings.length; i < l; i++) {
alert(regularExpression + ' matches ' + testStrings[i] + ': ' +
regularExpression.test(testStrings[i]));
}
}

Error:"Unexpected quantifier"
This error is generated using the general expression /^\d{3}\*?$/.

Well, I get the same error in MSIE with

/^d{3}*?$/

in stead of

/^\d{3}\*?$/

(so, without the backslashes). 'd' in stead of '\d' is no problem, but
repetition quantifiers like '*' can't refer to something like 'd{3}'.

Do you see those errors as well on http://dotinternet.be/temp/test.htm
? Is /^\d{3}\*?$/ the actual code that you see in browser view
source, or is it without the slashes there ? How do you generate the
javascript code ? In an .htm(l) page or via server script ?

--
Bart
Thank you for your response.
The http://--- you gave goes directly to two alerts, but the matches
read true for both:
"/^\d{3}\*?$/ matches 214: true" and "/^\d{3}\*?$/ matches 215*:
true".

My browser>view>source shows the following:

function labstat(){

var testStrings = [ '214', '215*'];
var regularExpression = /^d{3}*?
--------
--------

It has taken out the backslash before "*" as well as "$/" at the end of
the expression.
I am using Perl (cgi) script with javascript functions written between
"my $JSCRIPT = <<EOF;" and "EOF;".

So what do you make of it?
lev

Sep 14 '06 #6

P: n/a
lev wrote:
The http://--- you gave goes directly to two alerts, but the matches
read true for both:
"/^\d{3}\*?$/ matches 214: true" and "/^\d{3}\*?$/ matches 215*:
true".
Yes, that is what it should say.
My browser>view>source shows the following:

function labstat(){

var testStrings = [ '214', '215*'];
var regularExpression = /^d{3}*?
--------
--------

It has taken out the backslash before "*" as well as "$/" at the end of
the expression.
I am using Perl (cgi) script with javascript functions written between
"my $JSCRIPT = <<EOF;" and "EOF;".
Replacing every \ by \\ should solve your problem. Alternatively, use
single quotes to start your here-document:

my $JSCRIPT = <<'EOF'

--
Bart

Sep 14 '06 #7

P: n/a


lev wrote:

The http://--- you gave goes directly to two alerts, but the matches
read true for both:
"/^\d{3}\*?$/ matches 214: true" and "/^\d{3}\*?$/ matches 215*:
true".
The result is fine as both strings have three digits followed by an
optional '*'.

If you don't get any syntax error/JavaScript errors then Bart has
established that the problem you encounter with your attempts are
probably not caused by a buggy script engine but rather by real syntax
errors in the JavaScript code you have or produce. Can you post a URL
where you have your faulty expression?


--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 14 '06 #8

P: n/a
lev wrote:
[...]
My browser>view>source shows the following:
function labstat(){

var testStrings = [ '214', '215*'];
var regularExpression = /^d{3}*?
--------
It has taken out the backslash before "*" as well as "$/" at the end of
the expression.
Perl considers $/ ($RD, $INPUT_RECORD_SEPARATOR) as a special internal
variable that indicates the boundary between input lines. Actually, all
$, @ and \ need to be escaped in non-interpolating assignment
notations.

my $JSCRIPT = <<'EOF'

is one possible literal assignment notation, so no extra escape
sequences are needed in it. Two common alternatives:

my $JSCRIPT = q{};
my $JSCRIPT = '';

More info:
http://en.wikipedia.org/wiki/Concate...#Interpolation

--
Bart

Sep 14 '06 #9

P: n/a
lev
Bart Van der Donck wrote:
lev wrote:
[...]
My browser>view>source shows the following:
function labstat(){

var testStrings = [ '214', '215*'];
var regularExpression = /^d{3}*?
--------
It has taken out the backslash before "*" as well as "$/" at the end of
the expression.

Perl considers $/ ($RD, $INPUT_RECORD_SEPARATOR) as a special internal
variable that indicates the boundary between input lines. Actually, all
$, @ and \ need to be escaped in non-interpolating assignment
notations.

my $JSCRIPT = <<'EOF'

is one possible literal assignment notation, so no extra escape
sequences are needed in it. Two common alternatives:

my $JSCRIPT = q{};
my $JSCRIPT = '';

More info:
http://en.wikipedia.org/wiki/Concate...#Interpolation

--
Bart


Applying single quotes to my here-document (my $JSCRIPT = <<'EOF') as
advised seems to have solved the problem. The numbers 214 and 215* are
distinguished from one another by the asterisk when '/^\d{3}\*$/'
rather than '/^\d{3}\*?$/' (with ? option) is used.
Correspondingly, 'view>source' shows the regular expression as written
in the script.

Thank you Bart for the keen Perl insight and thank you Martin for the
initial help with a compact script and regexp that made my problem more
pointed and clear.

all the best,
lev

Sep 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.