Regex Javascript. Why are my curly braces not working?

时间:2015-06-26 10:13:09

标签: javascript regex

I am very confused as to why the curly braces seem to be ignored in my code? However if I run the command in the console it seems to work fine.

datetime: function(field) {
  var regEx = /^\d{1,2}$/;

  if (field.val().length > 0) {

    // This works in the console /^\d{1,2}$/.test("22");
    console.log(regEx) // /^\dUNDEF$/
    console.log(typeof regEx); // Object
    console.log(typeof field.val()); // String
    return regEx.test(field.val());
  } else {
    return true;
  }
}

1 个答案:

答案 0 :(得分:3)

Thanks to Frédéric Hamidi, a light bulb went off in my head.

The reason the curly braces are coming back UNDEF is due to the Java backend tagging system. I should keep this Javascript away from the template in its own file!!

Thanks Frédéric Hamidi :)