Regular expressions again

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    Regular expressions again

    I must be missing something, but I don't know what...

    alert( source.indexOf( "source") );
    var e3="\/source\/ig";
    source=source.r eplace( e3, "pqrst" );
    alert( source.indexOf( "pqrst") );

    The first alert shows 8937, so the substring exists. Therefore,
    "pqrst" must appear no later than 8937 in source, right? So why in
    the world would I get 9091 for the second alert?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Christopher Benson-Manica

    #2
    Re: Regular expressions again

    Christopher Benson-Manica <ataru@nospam.c yberspace.org> spoke thus:
    [color=blue]
    > I must be missing something, but I don't know what...[/color]

    Just to be sure...

    <html>
    <body>
    <script type="text/javascript">
    var source="abcdsou rcebcdefjkwerjk vawerjbksourcep qrstpqrstpqrst" ;
    alert( source.indexOf( "source") );
    var e3="\/source\/gi";
    source=source.r eplace( e3, "pqrst" );
    alert( source.indexOf( "source") );
    </script>
    </body></html>

    So I guess the problem is those stupid backslashes. They're there to
    make sure the page makes it through the W3C's markup validator. So
    how do I both keep the validator happy and actually get this stupid
    regex replacement to work? TIA...

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

    Comment

    • Grant Wagner

      #3
      Re: Regular expressions again

      Christopher Benson-Manica wrote:
      [color=blue]
      > I must be missing something, but I don't know what...
      >
      > alert( source.indexOf( "source") );
      > var e3="\/source\/ig";[/color]

      That isn't how you assign a literal regexp. Either use:

      var e3 = /source/ig;

      or use:

      var e3 = new RegExp("source" , "ig");

      Note that in this second case, regular String escape sequences apply. In other
      words:

      var myRegExp = /\d{4}/;
      var myRegExp = new RegExp("\\d{4}" );

      are equivalent.

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • Christopher Benson-Manica

        #4
        Re: Regular expressions again

        Grant Wagner <gwagner@agrico reunited.com> spoke thus:
        [color=blue]
        > var e3 = new RegExp("source" , "ig");[/color]

        Should have thought of it myself; perhaps it's time to call it a
        week... Thank you kindly.

        --
        Christopher Benson-Manica | I *should* know what I'm talking about - if I
        ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        Working...