This answer will not work except in the 700 case because the OP specified that all prefixes were followed by 6 digits, when in fact only 93700 is followed by 6, and the other prefixes are followed by 7 digits.var numbers = "(123) 456-7890".replace(/[^\d]/g, ""); //This strips all characters that aren't digits if (numbers.length ! The script contains a form to allow visitors to send an SMS to phone numbers.
Regular expressions can be used to perform all types of text search and text replace operations.
The international accounts for an optional initial ' ' and country code. Valid matches would be: My gut feeling is reinforced by the amount of replies to this topic - that there is a virtually infinite number of solutions to this problem, none of which are going to be elegant.
If you're talking about form validation, the regexp to validate correct meaning as well as correct data is going to be extremely complex because of varying country and provider standards. I interpret the question as looking for a broadly valid pattern, which may not be internally consistent - for example having a valid set of numbers, but not validating that the trunk-line, exchange, etc. North America is straightforward, and for international I prefer to use an 'idiomatic' pattern which covers the ways in which people specify and remember their numbers: The North American pattern makes sure that if one parenthesis is included both are.
The replace() method returns a modified string where the pattern is replaced. It searches a string for a pattern, and returns true or false, depending on the result.
The following example searches a string for the character "e": The exec() method is a Reg Exp expression method.