Loading ...
Sorry, an error occurred while loading the content.

Help on "bad escapement" regex error

Expand Messages
  • mars77
    This seems to be producing Bad escapement error results = str.replace(new RegExp( $ $([a-z]+) , gim ), {%=$1%} ); as you can see I am looking to
    Message 1 of 3 , Jul 29, 2009
    • 0 Attachment
      This seems to be producing "Bad escapement error"
      results = str.replace(new RegExp('\\\$\\\$([a-z]+)', 'gim'), "{%=$1%}");

      as you can see I am looking to replace everything \$\$foo
      What escapement issue is this?

      I am not really sure how to fix that?
    • Douglas Crockford
      ... You are seeing an interaction between RegExp escapement and string escapement. String escapement happens first, so you an either write results =
      Message 2 of 3 , Jul 29, 2009
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "mars77" <mars77@...> wrote:
        >
        > This seems to be producing "Bad escapement error"
        > results = str.replace(new RegExp('\\\$\\\$([a-z]+)', 'gim'), "{%=$1%}");
        >
        > as you can see I am looking to replace everything \$\$foo
        > What escapement issue is this?

        You are seeing an interaction between RegExp escapement and string escapement.

        String escapement happens first, so you an either write

        results = str.replace(new RegExp('\\\\\\$\\\\\\$([a-z]+)', 'gim'), "{%=$1%}");

        or better,

        results = str.replace(/\\\$\\\$([a-z]+)/gim, "{%=$1%}");

        You should avoid the RegExp constructor.
      • mars77
        that works. Thank you very much.
        Message 3 of 3 , Jul 29, 2009
        • 0 Attachment
          that works.

          Thank you very much.
        Your message has been successfully submitted and would be delivered to recipients shortly.