javascript regular expression methods, problem with escape characters

L

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
 
M

Martin Honnen

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 + ': ' +
regularExpression.test(testStrings));
}

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.
 
B

Bart Van der Donck

lev said:
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.
 
L

lev

Martin said:
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 + ': ' +
regularExpression.test(testStrings));
}

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 + ': ' +
regularExpression.test(testStrings));
}

}

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
 
B

Bart Van der Donck

lev said:
[...]
function labstat(){

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

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 ?
 
L

lev

Bart said:
lev said:
[...]
function labstat(){

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

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 ?


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
 
B

Bart Van der Donck

lev said:
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'
 
M

Martin Honnen

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?
 
B

Bart Van der Donck

lev said:
[...]
My browser>view>source shows the following:
function labstat(){

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

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/Concatenation#Interpolation
 
L

lev

Bart said:
lev said:
[...]
My browser>view>source shows the following:
function labstat(){

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

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/Concatenation#Interpolation



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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top