javascript regular expression methods, problem with escape characters

Discussion in 'Javascript' started by lev, Sep 13, 2006.

  1. lev

    lev Guest

    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
     
    lev, Sep 13, 2006
    #1
    1. Advertising

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





    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 13, 2006
    #2
    1. Advertising

  3. 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
     
    Bart Van der Donck, Sep 13, 2006
    #3
  4. lev

    lev Guest

    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 + ': ' +
    > 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
     
    lev, Sep 14, 2006
    #4
  5. 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 + ': ' +
    > 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 ?

    --
    Bart
     
    Bart Van der Donck, Sep 14, 2006
    #5
  6. lev

    lev Guest

    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 + ': ' +
    > > 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 ?
    >
    > --
    > 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
     
    lev, Sep 14, 2006
    #6
  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".


    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
     
    Bart Van der Donck, Sep 14, 2006
    #7
  8. 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/
     
    Martin Honnen, Sep 14, 2006
    #8
  9. 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/Concatenation#Interpolation

    --
    Bart
     
    Bart Van der Donck, Sep 14, 2006
    #9
  10. lev

    lev Guest

    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/Concatenation#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
     
    lev, Sep 17, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,311
  2. aefxx
    Replies:
    1
    Views:
    595
    Virgil Green
    Dec 17, 2004
  3. slomo
    Replies:
    5
    Views:
    1,549
    Duncan Booth
    Dec 2, 2007
  4. compboy
    Replies:
    7
    Views:
    105
    Uri Guttman
    May 28, 2006
  5. bharath_r

    Escape .(dot) in a Regular Expression

    bharath_r, Nov 17, 2008, in forum: Javascript
    Replies:
    2
    Views:
    160
    Thomas 'PointedEars' Lahn
    Nov 17, 2008
Loading...

Share This Page