Your pattern
must be RegEx literal (double quotes around that should not be there) like this
var pattern = /^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/;
Otherwise you need to use RegExp
object, with proper escaping for \
, like this
var pattern = new RegExp("^(()?\\d{3}())?(-|\\s)?\\d{3}(-|\\s)?\\d{4}$");
Related Contents:
- Fastest method to escape HTML tags as HTML entities?
- How can I validate an email address in JavaScript?
- Strip HTML from Text JavaScript
- How to count string occurrence in string?
- How to remove all line breaks from a string
- How can I extract a number from a string in JavaScript?
- Converting user input string to regular expression
- Get Substring between two characters using javascript
- Replace multiple strings with multiple other strings
- Split large string in n-size chunks in JavaScript
- Regular Expression to get a string between parentheses in Javascript
- Regular expression to get a string between two strings in Javascript
- Convert camelCaseText to Title Case Text
- Create a string of variable length, filled with a repeated character
- Split string on the first white space occurrence
- How to extract a string using JavaScript Regex?
- How to extract base URL from a string in JavaScript?
- Use dynamic (variable) string as regex pattern in JavaScript
- How to find indices of all occurrences of one string in another in JavaScript?
- Javascript regex returning true.. then false.. then true.. etc [duplicate]
- Insert space before capital letters
- Regular expression which matches a pattern, or is an empty string
- javascript regular expression to not match a word
- In Javascript, how can I perform a global replace on string with a variable inside ‘/’ and ‘/g’?
- Check for special characters in string
- Difference between toLocaleLowerCase() and toLowerCase() [duplicate]
- javascript regular expression to check for IP addresses
- How do I split a string by whitespace and ignoring leading and trailing whitespace into an array of words using a regular expression?
- javascript url-safe filename-safe string
- JavaScript: indexOf vs. Match when Searching Strings?
- Javascript – return string between square brackets
- Removing all script tags from html with JS Regular Expression
- Javascript string replace with regex to strip off illegal characters
- How do I escape a string inside JavaScript code inside an onClick handler?
- Extract the text out of HTML string using JavaScript
- How to prevent Javascript injection attacks within user-generated HTML
- Passing regex modifier options to RegExp object
- Strip HTML tags from text using plain JavaScript
- RegEx that will match the last occurrence of dot in a string
- Replacing accented characters with plain ascii ones [duplicate]
- How to wrap part of a text in a node with JavaScript
- Remove first character from a string if it is a comma
- Remove whitespace and line breaks between HTML elements using jQuery
- Split a string based on multiple delimiters
- Include the hyphen into this regular expression, how?
- Concrete JavaScript regular expression for accented characters (diacritics)
- Getting binary (base64) data from HTML5 Canvas (readAsBinaryString)
- Is it bad practice to embed JavaScript into the body of HTML?
- filereader api on big files
- “The stylesheet was not loaded because its MIME type, “text/html” is not “text/css” [closed]