I have a regex that already takes care of standardizing formatting of U.S. phone numbers, however it doesn't deal with a leading 1.
var cleanTelephoneNumber = function(tel) {
var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if (regexObj.test(tel)) {
return tel.replace(regexObj, "($1) $2-$3");
} else {
return null;
}
};
how can I get it to strip out a leading one if it exists and still continue to parse correctly
e.g.
+1-555-235-2444
1-555-235-2444
1.555.235-2444
1 555 235 2444
555-235-2444
should all translate to
(555) 235-2444
I'd like to just modify the regex I already have
/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/
You can modify your regex to use this:
^(?:\+?1?[-.\s]?)(\d{3})([-.\s])(\d{3})\2(\d{4})$
Working demo
The idea of the regex is:
^(?:\+?1?[-.\s]?) can have +1 and a separator
(\d{3}) must contain 3 digits
([-.\s]) store a separator
(\d{3}) follow by 3 digits
\2 use the same separator
(\d{4})$ follow by 4 digits
Related
This question already has answers here:
IBAN Validation check
(11 answers)
Closed 4 years ago.
Help me please to design Regex that will match all IBANs with all possible whitespaces. Because I've found that one, but it does not work with whitespaces.
[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}
I need at least that formats:
DE89 3704 0044 0532 0130 00
AT61 1904 3002 3457 3201
FR14 2004 1010 0505 0001 3
Just to find the example IBAN's from those countries in a text :
Start with 2 letters then 2 digits.
Then allow a space before every 4 digits, optionally ending with 1 or 2 digits:
\b[A-Z]{2}[0-9]{2}(?:[ ]?[0-9]{4}){4}(?!(?:[ ]?[0-9]){3})(?:[ ]?[0-9]{1,2})?\b
regex101 test here
Note that if the intention is to validate a complete string, that the regex can be simplified.
Since the negative look-ahead (?!...) won't be needed then.
And the word boundaries \b can be replaced by the start ^ and end $ of the line.
^[A-Z]{2}[0-9]{2}(?:[ ]?[0-9]{4}){4}(?:[ ]?[0-9]{1,2})?$
Also, it can be simplified even more if having the 4 groups of 4 connected digits doesn't really matter.
^[A-Z]{2}(?:[ ]?[0-9]){18,20}$
Extra
If you need to match an IBAN number from accross the world?
Then the BBAN part of the IBAN is allowed to have up to 30 numbers or uppercase letters. Reference
And can be written with either spaces or dashes or nothing in between.
For example: CC12-XXXX-12XX-1234-5678-9012-3456-7890-123
So the regex pattern to match a complete string with a long IBAN becomes a bit longer.
^([A-Z]{2}[ \-]?[0-9]{2})(?=(?:[ \-]?[A-Z0-9]){9,30}$)((?:[ \-]?[A-Z0-9]{3,5}){2,7})([ \-]?[A-Z0-9]{1,3})?$
regex101 test here
Also note, that a pure regex solution can't do calculations.
So to actually validate an IBAN number then extra code is required.
Example Javascript Snippet:
function smellsLikeIban(str){
return /^([A-Z]{2}[ \-]?[0-9]{2})(?=(?:[ \-]?[A-Z0-9]){9,30}$)((?:[ \-]?[A-Z0-9]{3,5}){2,7})([ \-]?[A-Z0-9]{1,3})?$/.test(str);
}
function validateIbanChecksum(iban) {
const ibanStripped = iban.replace(/[^A-Z0-9]+/gi,'') //keep numbers and letters only
.toUpperCase(); //calculation expects upper-case
const m = ibanStripped.match(/^([A-Z]{2})([0-9]{2})([A-Z0-9]{9,30})$/);
if(!m) return false;
const numbericed = (m[3] + m[1] + m[2]).replace(/[A-Z]/g,function(ch){
//replace upper-case characters by numbers 10 to 35
return (ch.charCodeAt(0)-55);
});
//The resulting number would be to long for javascript to handle without loosing precision.
//So the trick is to chop the string up in smaller parts.
const mod97 = numbericed.match(/\d{1,7}/g)
.reduce(function(total, curr){ return Number(total + curr)%97},'');
return (mod97 === 1);
};
var arr = [
'DE89 3704 0044 0532 0130 00', // ok
'AT61 1904 3002 3457 3201', // ok
'FR14 2004 1010 0505 0001 3', // wrong checksum
'GB82-WEST-1234-5698-7654-32', // ok
'NL20INGB0001234567', // ok
'XX00 1234 5678 9012 3456 7890 1234 5678 90', // only smells ok
'YY00123456789012345678901234567890', // only smells ok
'NL20-ING-B0-00-12-34-567', // stinks, but still a valid checksum
'XX22YYY1234567890123', // wrong checksum again
'droid#i.ban' // This Is Not The IBAN You Are Looking For
];
arr.forEach(function (str) {
console.log('['+ str +'] Smells Like IBAN: '+ smellsLikeIban(str));
console.log('['+ str +'] Valid IBAN Checksum: '+ validateIbanChecksum(str))
});
Here is a suggestion that may works for the patterns you provided:
[A-Z]{2}\d{2} ?\d{4} ?\d{4} ?\d{4} ?\d{4} ?[\d]{0,2}
Try it on regex101
Explanation
[A-Z]{2}\d{2} ? 2 capital letters followed by 2 digits (optional space)
\d{4} ? 4 digits, repeated 4 times (optional space)
[\d]{0,2} 0 to 2 digits
You can use a regex like this:
^[A-Z]{2}\d{2} (?:\d{4} ){3}\d{4}(?: \d\d?)?$
Working demo
This will match only those string formats
It's probably best to look up the specifications for a correct IBAN number. But if you want to have a regex similar to your existing one, but with spaces, you can use the following one:
^[a-zA-Z]{2}[0-9]{2}\s?[a-zA-Z0-9]{4}\s?[0-9]{4}\s?[0-9]{3}([a-zA-Z0-9]\s?[a-zA-Z0-9]{0,4}\s?[a-zA-Z0-9]{0,4}\s?[a-zA-Z0-9]{0,4}\s?[a-zA-Z0-9]{0,3})?$
Here is a live example: https://regex101.com/r/ZyIPLD/1
I want to get a set of numbers (3 digits) from a string. But some numbers bind with specific text and those numbers don't need to be include in the output.
Input:
C123456 577 abcd 173944 C5678541883
Result should to be:
577 173 944 188
How can I achieve this?
I assume, you only want to match full numbers without any other characters, separated by white space. If you really want to capture and 188 too and 173944 split into two parts, you can provide a comment below with more information about what it actually should match (numbers with 3 or 6 characters, numbers etc.)
So, to extract all numbers from a white-space separated string, you can use the following regular expression. Make sure to use the global flag /g:
\b([0-9]+)\b
Here is a live example:
var text = "C123456 577 abcd 173944 C5678541883";
var regex = /\b([0-9]+)\b/g;
var match = regex.exec(text);
while(match !== null) {
console.log(match[1]);
match = regex.exec(text);
}
i want to validate my phone number with the regex for following formats.i have googled the things but i could not find the regex for following formats...
079-26408300 / 8200
(079) 26408300
079 264 083 00
9429527462
can anyone please guide me how can i do validate the phone number field for above formats?
I want to validate the phone number for only above formats as right now am using only following regex var phone_pattern = /^[a-z0-9]+$/i;
#Ali Shah Ahmed
var phone_pattern = "(\d{10})|(\d{3}-\d{8}\s/\s\d{4})|((\d{3}\s){3}\d{2})|((\d{3})\s\d{8})";
here is the way am checking if its valid
if (!phone_pattern.test(personal_phone))
{
$("#restErrorpersonalphone").html('Please enter valid phone number');
$("#personal_phone").addClass('borderColor');
flag = false;
} else {
$("#restErrorpersonalphone").html('');
$("#personal_phone").removeClass('borderColor');
}
its not working. Am I implementing in wrong way?
lets start with the simplest phone number 9429527462
As this has 10 characters and all are numbers, regex for it could be \d{10}
Now the next phone number 079 264 083 00. Regex for this pattern could be (\d{3}\s){3}\d{2}
First we are expecting a group of 3 digits and a space to repeat thrice (\d{3}\s){3}, this will cover 079 264 083 (space included in it), so left will be the last two characters which are handled using \d{2}
For the phone number (079) 26408300, \(\d{3}\)\s\d{8} regex could be use. The regex first looks for a opening bracket, then three digits inside it, and then the closing bracket. It then looks for a space, and then for 8 digits.
The phone number 079-26408300 / 8200 could be validated using regex \d{3}-\d{8}\s/\s\d{4}. It first looks for 3 digits then a -, then 8 digits followed by a space. Then looks for a / and then a space and then 4 digits.
If you wish to know a single regex for validating all the above patterns, do let me know.
Final combined regex would be:
/(\d{10})|(\d{3}-\d{8}\s\/\s\d{4})|((\d{3}\s){3}\d{2})|(\(\d{3}\)\s\d{8})/
Straightforward solution is simple, use |
String ex = "\\d{3}-\\d{8} / \\d{4}|\\(\\d{3}\\) \\d{8}|...
The text looks like this:
"Beginning. 1. The container is 1.5 meters long 2. It can hold up to 2lt of fluid. 3. It 4 holes."
There may not be a dot at the end of each list element.
How can I split this text into a list as shown below?
"Beginning."
"The container is 1.5 meters long"
"It can hold up to 2lt of fluid."
"It has 4 holes."
In other words I need to match (\d+)\. such that all (\d+) are consecutive integers so that I can split and trim the text between them. Is it possible with regex? How far do I have to venture into the realm of computer science?
Use
\d+\.(?!\d)
as the splitting regex, i. e. in PHP
$result = preg_split('/\d+\.(?!\d)/', $subject);
The negative lookahead (?!\d) ensures that no digit follows after the dot has been matched.
Or make the spaces mandatory - if that's an option:
$result = preg_split('/\s+\d+\.\s+/', $subject);
This is working c# code:
string s = "Beginning. 1. The container is 1.5 meters long 2. It can hold up to 2lt of fluid. 3. It has 4 holes.";
string[] res = Regex.Split(s, #"\s*\d+\.\s+");
foreach (var r in res)
{
Console.WriteLine(r);
}
Console.ReadLine();
I split on \s*\d+\.\s+ that means optional white space, followed by at least one digit ,followed by a dot, then at least one whitespace.
I'm looking for a simple regex that will validate a 10 digit phone number. I'd like to make sure that the number is exactly 10 digits, no letters, hyphens or parens and that the first two digits do not start with 0 or 1. Can someone help out?
/[2-9]{2}\d{8}/
^[2-9]{2}[0-9]{8}$
I consider [0-9] to be better to read than \d, especially considering the preceding [2-9]
The ^ and $ ensure that the input string consists ONLY of those 8 characters - otherwise it is not guaranteed that the input string is not larger - i.e. "12345678901" would match the regex w/o those two characters - although it is 11 chars and starts with a 1!
As Randal pointed out, this question is not consistent with the way phone numbers are formatted in North America (even though the OP stated 'first two digits do not start with 0 or 1'). A better regex for North American phone numbers would be:
^[2-9]{1}[0-9]{9}$
For example, Washington DC's area code is (202). NYC has area code (212). Northern New Jersey has (201).
But more accurately, the NANP has a lot of rules as it relates to what is allowed in area code and exchange (first six digits). This regex should still cover most cases. https://en.wikipedia.org/wiki/North_American_Numbering_Plan
This regex script might help out. I essentially strips any "punctuation" characters, including a leading 1-, then validates it is 10 digits.
The extra part you probably don't need is the formatting to 000-000-0000
formatPhone = function() {
var phone = this.value;
phone = phone.replace(/^1(|-|\(|\)|\.| )*|-|\(|\)|\.| /g, '');
if(phone.length === 10) {
this.value = phone.slice(0,3) + '-' + phone.slice(3,6) + '-' + phone.slice(6,10);
}
}
The Phone Numbers will be of 10 digits, and it will start from 7,8 and 9
[RegularExpression("^([07][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 8[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 9[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])$", ErrorMessage = "Enter Valid Mobile Number")]
reference : http://www.regular-expressions.info/numericranges.html