473,466 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

javascript regular expression methods, problem with escape characters

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
9 5691


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


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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.