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

  2. 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, Jul 19, 2009
    #2
    1. Advertisements

  3. Ciaran

    cronoklee Guest

    Great thanks - doubling the backslashes worked well!

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

    song Guest

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

    console.log(str.replace(reg,"xxx"));
     
    song, Jul 20, 2009
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.