Regexp replace behaving strangely?

Discussion in 'Javascript' started by Ciaran, Jul 19, 2009.

  1. Ciaran

    Ciaran Guest

    Hi This replacement is doing funny things:

    var stringObject='<p>[[:HTML some html code:]] </p>';
    var patt1=new RegExp("\[\[:html(.*?):]]",'gi');
    alert(stringObject.replace(patt1,"XXX"));

    I want it to alert:
    <p>XXX </p>

    But it's alerting:
    <p>[[:HTML some html codeXXX] </p>

    Can anyone help me out?

    Thanks,
    Ciarán
     
    Ciaran, Jul 19, 2009
    #1
    1. Advertising

  2. Ciaran wrote:
    > Hi This replacement is doing funny things:
    >
    > var stringObject='<p>[[:HTML some html code:]] </p>';
    > var patt1=new RegExp("\[\[:html(.*?):]]",'gi');
    > alert(stringObject.replace(patt1,"XXX"));
    >
    > I want it to alert:
    > <p>XXX </p>
    >
    > But it's alerting:
    > <p>[[:HTML some html codeXXX] </p>
    >
    > Can anyone help me out?


    Use a regular expression literal or make sure you double any backslashes
    as you need one for the string literal and one for the regular
    expression language:
    var patt1=new RegExp("\\[\\[:html(.*?):]]",'gi');

    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Jul 19, 2009
    #2
    1. Advertising

  3. Ciaran

    cronoklee Guest

    On Jul 19, 4:28 pm, Martin Honnen <> wrote:

    > Use a regular expression literal or make sure you double any backslashes
    > as you need one for the string literal and one for the regular
    > expression language:
    >    var patt1=new RegExp("\\[\\[:html(.*?):]]",'gi');


    Great thanks - doubling the backslashes worked well!

    Ciarán
     
    cronoklee, Jul 19, 2009
    #3
  4. Ciaran

    song Guest

    Ciaran <> writes:

    var str = "<p>[[:HTML some html code:]]</p>";
    var reg = /\[\[:html(.*?):\]\]/i;

    console.log(str.replace(reg,"xxx"));

    > Hi This replacement is doing funny things:
    >
    > var stringObject='<p>[[:HTML some html code:]] </p>';
    > var patt1=new RegExp("\[\[:html(.*?):]]",'gi');
    > alert(stringObject.replace(patt1,"XXX"));
    >
    > I want it to alert:
    > <p>XXX </p>
    >
    > But it's alerting:
    > <p>[[:HTML some html codeXXX] </p>
    >
    > Can anyone help me out?
    >
    > Thanks,
    > Ciarán
     
    song, Jul 20, 2009
    #4
    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. =?iso-8859-1?q?Nils Hedstr=f6m

    Stateserver behaving strangely

    =?iso-8859-1?q?Nils Hedstr=f6m, Feb 11, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,042
    =?iso-8859-1?q?Nils Hedstr=f6m
    Feb 11, 2005
  2. Josh Close

    re behaving strangely

    Josh Close, Jun 9, 2005, in forum: Python
    Replies:
    2
    Views:
    300
    Josh Close
    Jun 9, 2005
  3. Travis Whitton

    IO.seek behaving strangely on FreeBSD 4.9

    Travis Whitton, Apr 1, 2004, in forum: Ruby
    Replies:
    5
    Views:
    211
    Yukihiro Matsumoto
    Apr 2, 2004
  4. Jadhav, Alok
    Replies:
    9
    Views:
    321
    Thomas Rachel
    Nov 15, 2012
  5. Jadhav, Alok
    Replies:
    0
    Views:
    258
    Jadhav, Alok
    Sep 17, 2012
Loading...

Share This Page