A user can fill a phone number. ( only digits and dashes , dashes are not mandatory)
He can have as much (middle) dashes (-) but the total count of digits must be 10.
I've managed writing a regex using positive lookahead of "-" in numbers :
^(?=.*\-)[0-9\-]+$
But I have 2 problems with that :
the dash ( in my regex) can be also in the beginning and at the end and that's not valid.
I haven't succeed applying the 10 digits restrictions.
p.s. examples of valid examples :
050-6783828
050-678-38-28
0506783828
not valid :
-0506783826
0506783826-
050678--3826
p.s.2 please notice this question is tagged as regex. I'm not looking for js (non-regex) solutions.
I think you want something like this:
^\d(?:-?\d){9}$
Start with a digit.
9 times: optional dash and another digit.
Working example: http://rubular.com/r/CrgTOrXC8E
^[0-9](-?[0-9]){8}-?[0-9]$
A digit at the begin and end, 8 groups of optional dash and digit, plus optional dash before last digit
Only one dash is allowed between eatch neighbouring digits.
var pat = new RegExp('^[0-9](-?[0-9]){8}-?[0-9]$')
// correct
console.log(pat.test('0506783828'))
console.log(pat.test('0-5-0-6-7-8-3-8-2-8'))
// incorrect
console.log(pat.test('0506783828-'))
console.log(pat.test('-0506783828'))
console.log(pat.test('05--06783828'))
Related
I need to extract certain part of Javascript string. I was thinking to do it with regex, but couldn't come up with one which does it correctly.
String can have variable length & can contain all possible characters in all possible combinations.
What I need to extract from it, is 10 adjacent characters, that match one of next two possible combinations:
9 numbers & 1 letter "X" (capital letter "X", not X as variable letter!)
10 numbers
So, if input string is this: "[1X,!?X22;87654321X9]ddee", it should return only "87654321X9".
I hope I've explained it good enough. Thanks in advance!
This Regex will work:
\d{9}X|\d{8}X\d|\d{7}X\d{2}|\d{6}X\d{3}|\d{5}X\d{4}|\d{4}X\d{5}|\d{3}X\d{6}|\d{2}X\d{7}|\d{1}X\d{8}|\d{10}|X\d{9}
As described, It need to match 9 digits and any letter, and the letter can be at any position of the sequence.
\d{9}X # will match 9 digits and a letter in the end
\d{8}X\d # will match 8 digits a lettter then a digit again
...
\d{1}X\d{8} # will match 1 digits a lettter then 8 digits
\{10} # will match 10 digits
Edited to match only X
You can use this much simpler regex:
/(?!\d*X\d*X)[\dX]{10}/
RegEx Breakup:
(?!\d*X\d*X) # negative lookahead to fail the match if there are 2 X ahead
[\dX]{10} # match a digit or X 10 times
Since more than one X is not allowed due to use of negative lookahead, this regex will only allow either 10 digits or ekse 9 digits and a single X.
RegEx Demo
This regex has few advantages over the other answer:
Much simpler regex that is easier to read and maintain
Takes less than half steps to complete which can be substantial difference on larger text.
I would like some help with this regex in Javascript I'm working with where users type a ticket number into chat.
I am working with the following possibilities and the regex TTN-\d{1,4}?([0-9]{1,9}).
The number of digits possible is 10 but sometimes users will not include the leading zeroes so I need to be sure to match strings with up to 10 digits; however, the issue I am having is that it will still capture if there are greater than 10 digits.
I want to match against the follow here and capture the digits after "TTN-"
random word TTN-484424 random word - I would capture 4844254. Works good.
TTN-0000846110 - I would capture 0000846110. Works good.
hey look at TTN-0000844555 blah blah - I would capture 0000844555. Works good.
random word TTN-00099999990980 random word - I would not want to match this string because the number of digits is greater than 10; therefore, the user incorrectly typed the ticket number.
I've been reading up on regex tutorials and tried using `$' to signify the end of the string but that doesn't work since it still goes on in length.
How can I signify the regex to only start matching when the digits of the string are <= 10?
Thank you!
Use word boundaries.
TTN-(\d{1,10})\b
\d{1,10} matches 1 to 10 digit chars.
\b word boundary which matches between a word character and a non-word character.
Don't allow a digit after up to 10.
TTN-(\d{1,10})(?!\d)
I have strings like
XXX-1234
XXXX-1234
XX - 4321
ABCDE - 4321
AB -5677
So there will be letters at the beginning. then there will be hyphen. and then 4 digits. Number of letters may vary but number of digits are same = 4
Now I need to match the first 2 positions from the digits. So I tried a long process.
temp_digit=mystring;
temp_digit=temp_digit.replace(/ /g,'');
temp_digit=temp_digit.split("-");
if(temp_digit[1].substring(0,2)=='12') {}
Now is there any process using regex / pattern matching so that I can do it in an efficient way. Something like string.match(regexp) I'm dumb in regex patterns. How can I find the first two digits from 4 digits from above strings ? Also it would be great it the solution can match digits without hyphens like XXX 1234 But this is optional.
Try a regular expression that finds at least one letter [a-zA-Z]+, followed by some space if necessary \s*, followed by a hyphen -, followed by some more space if necessary \s*. It then matches the first two digits \d{2} after the pattern.:
[a-zA-Z]+\s*-\s*(\d{2})
may vary but number of digits are same = 4
Now I need to match the first 2 positions from the digits.
Also it would be great it the solution can match digits without hyphens like XXX 1234 But this is optional.
Do you really need to check it starts with letters? How about matching ANY 4 digit number, and capturing only the first 2 digits?
Regex
/\b(\d{2})\d{2}\b/
Matches:
\b a word boundary
(\d{2}) 2 digits, captured in group 1, and assigned to match[1].
\d{2} 2 more digits (not captured).
\b a word boundary
Code
var regex = /\b(\d{2})\d{2}\b/;
var str = 'ABCDE 4321';
var result = str.match(regex)[1];
document.body.innerText += result;
If there are always 4 digits at the end, you can simply slice it:
str.trim().slice(-4,-2);
here's a jsfiddle with the example strings:
https://jsfiddle.net/mckinleymedia/6suffmmm/
so I got an assesment due and one the regular expressions is a phone digit that has 10 numbers but it also allows dashes "-". However it can only contain 2 dashes and start with 0. eg. 0x-xxxx-xxxx where x can be any digit between 0-9.
So, so far I've come up with the following regexes:
^[0-0][0-9-]{1,11}$
^[0-0][0-9-]{11}$
In the first one it works but allows any length, if I put 5 numbers it goes through as long as the first one is 0. In the second one I can put 2 dashes followed by each other or just fill it with dashes and it goes through.
Thanks for helping guys!
Match every of the three parts, separated by the dashes, like this:
^0\d-\d{4}-\d{4}$
This matches 0x-xxxx-xxxx. Demo: https://regex101.com/r/nW7wL5/1
If you also want to match the number without the dashes, use
^0\d-?\d{4}-?\d{4}$
Demo: https://regex101.com/r/gY0mC3/1
\d is the same as [0-9] but it's shorter.
I want a very simple Regex for Javascript validation of phone number, which allows 10 digits and checks min numbers should be 10 and max 12 including - dash two times for ex. 123-123-1234
I have found some on internet, but none of them is working for min / max length.
Looking forward for quick response here.
Thanks !
You could do this
/^(?!.*-.*-.*-)(?=(?:\d{8,10}$)|(?:(?=.{9,11}$)[^-]*-[^-]*$)|(?:(?=.{10,12}$)[^-]*-[^-]*-[^-]*$) )[\d-]+$/
See it here on Regexr
(?!...) is a negative lookahead assertion
(?=...) is a positive lookahead assertion
^ # Start of the string
(?!.*-.*-.*-) # Fails if there are more than 2 dashes
(?=(?:\d{8,10}$) # if only digits to the end, then length 8 to 10
|(?:(?=.{9,11}$)[^-]*-[^-]*$) # if one dash, then length from 9 to 11
|(?:(?=.{10,12}$)
[^-]*-[^-]*-[^-]*$ # if two dashes, then length from 10 to 12
)
)
[\d-]+ # match digits and dashes (your rules are done by the assertions)
$ # the end of the string
What you asking for wouldn't be a simple regular expression and also may be solved without any use 'em.
function isPhoneNumberValid(number){
var parts, len = (
parts = /^\d[\d-]+\d$/g.test(number) && number.split('-'),
parts.length==3 && parts.join('').length
);
return (len>=10 && len<=12)
}
For sure this may be a bit slower than using a compiled regex, but overhead is way minor if you not going to check hundreds of thousandths phone numbers this way.
This isn't perfect in any way but may fit your needs, beware however that this allow two dashes in any place excluding start and end of number, so this will return true for string like 111--123123.
There's no simple way to do this with regexp, especially if you allow dashes to appear at some different points.
If you allow dashes only at places as in your example, then it would be ^\d{3}-?\d{3}-?\d{4}$
This one: ^[\d-]{10,12}$ matches string with length from 10 to 12 and contains only digits and dashes, but it also will match e.g. -1234567890-.