Home > Blockchain >  How to include apostrophe ('), in the following regex expression in angular?
How to include apostrophe ('), in the following regex expression in angular?

Time:07-11

Following is regex expression for an address field which can contain (.), apostrophe ('), dash (-), number, (#), (@), ampersand (&), slash (/), and spaces.

But I am unable to add regex for an apostrophe (').

address: ['', [Validators.pattern('^[A-Za-z0-9- &@/#.] $')]],

CodePudding user response:

Since you are putting your regex pattern inside a single-quote delimited string, you must escape the single quote with a forward slash \':

Validators.pattern('^[A-Za-z0-9- &@/#.\'] $') // note the forward slash and a single quote

Or just change your string to a double-quote delimited string.

Validators.pattern("^[A-Za-z0-9- &@/#.'] $")

CodePudding user response:

Enclose the string passed to pattern() with a double quotes("") or backticks(``) and add an apostrophe(') inside the sqaure brackets[] in the regular expression,

address: ['', [Validators.pattern("^[A-Za-z0-9- &@/#.'] $")]]
  • Related