ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Escaping Backslashes for Javascript RegEx

February 18, 2011 5:42pm

Subscribe [2]
  • #1 / Feb 18, 2011 5:42pm

    bbormandev

    2 posts

    This is from ExpressionEngine v2.0.1pb01 Build:  20100215

    We ran into this issue recently while using a regular expression object in Javascript. Our regular expression was a rather simplified phone number validator:

    /\(?\d{3}[-\) ]?\d{3}[- ]?\d{4}/

    The regular expression object was being declared like this:

    var validPhoneExp = /\(?\d{3}[-\) ]?\d{3}[- ]?\d{4}/;

    When the template was saved all of the backslashes were removed from the regular expression leaving:

    /(?d{3}[-) ]?d{3}[- ]?d{4}/;

    Which is obviously not what we wanted. After some hair pulling and interesting debugging, we found what it was doing and got around this by escaping the backslashes like this:

    var validPhoneExp = /\\(?\\d{3}[-\\) ]?\\d{3}[- ]?\\d{4}/;

    And it works! The only issue is when we open the template again, it just has the one set of backslashes and so when we save it it removes those and leaves no backslashes again. I believe this is a bug due to escape characters when inserting the template into the database. I am unsure of what exactly is happening but it seems to be related to the MySql escape characters not being handled properly in a template.

  • #2 / Feb 20, 2011 1:22pm

    Greg Salt

    3988 posts

    Hi bbormandev,

    Can you please upgrade to the latest version of EE, which is 2.1.3. Additionally, we have a beta version of 2.1.4 available (you’ll see this as an option after selected the file package to download).

    Can you give us a little more information about where and how this JS is created and used? Is this a JS template, a script block on a webpage template or something else?

    Cheers

    Greg

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases