-1

I am trying to change the property "pattern" of my input using jquery to a regex pattern. this is the input :

<input id="phone" pattern="[0-9()#&amp;+*-=.]+">

I found a way, but the regex does not save the structure when inserted. this is what i have tried :

 var phoneField = jQuery('#phone');
 var phoneRe = "^0(?:[234689]|5[0-689]|7[246789])(?![01])(\d{7})$";
 phoneField.prop("pattern",phoneRe);

the output is : ^0(?:[234689]|5[0-689]|7[246789])(?![01])(d{7})$

expected output is : ^0(?:[234689]|5[0-689]|7[246789])(?![01])(\d{7})$

to be exact (d{7})$ should be (\d{7})$

davidasor
  • 199
  • 1
  • 14

1 Answers1

1

You will need to escape the escape character if the pattern is a string:

console.log(new RegExp("\d").test('5'));  // false
console.log(new RegExp("\\d").test('5')); // true
var phoneRe = "^0(?:[234689]|5[0-689]|7[246789])(?![01])(\\d{7})$";
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132