Reqular expression of 7 digits numbers with optional special characters in between - javascript

I need validate a 7 digit number with optional dash in between.
I was able to get if I use below.
^(\d-?\d-?\d-?\d-?\d-?\d-?\d)$
Is there a way to shorten that?
I tried ^(\d+(-?){7})$ but it's not working.
Valid 123-09-23
Valid 12-3092-3
Valid 1-230-9-23
Valid 1234567
Invalid -1237883
Invalid 12345678
InValid 123-45-678
PS: I will be implementing this in my Javascript application.

Repeat the group only (7 times, so you get 7 digits total), and don't repeat the \d as well (else you may match more digits than desired):
^(?:\d-?){7}$
https://regex101.com/r/yLQHWW/1
(Your original pattern is equivalent to: "Match one or more digits, optionally followed by up to 7 - characters".)

Start with a digit and repeat -?\d six times:
^\d(-?\d){6}$
https://regex101.com/r/oTSqri/1

Related

Javascript regular expression validate phone number

I need to validate phone number by regular expression. The phone number should
Start with (9|09|8869|+8869)
Followed by 8 digit [0-9]
I come up with /(09|9|8869|+8869)[0-9]{8}$/g.
I test with +8869900000000 and expect it will not match but actually it passed
Could you help me to address the regex problem? And how do I fix it?
You can use this regex: /^(0?9|\+?8869)\d{8}$/
The group (0?9|+?8869) is for your starting condition where 0 is optional before 9 and + is optional before 8869.
Demo: https://regex101.com/r/1OpYl0/1/
The regex you are looking for is : ^(([0]?9)|([+]?8869))[0-9]{8}$
Note the way round brackets used to determine the conditions. We need to match within any of the 2 subsets and then precede it 8 digits.

Javascript regex for extracting certain part of string

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.

Regular Expression formatting

I have an input box and the condition is to allow the user to enter only numbers, the numbers entered should be in the following format in groups of 4, ex: 4444 5555 and the maximum number of characters to be entered in the textbox should be 9. I am pretty new to regex, so have no clue of how to start. A working sample in fiddle would be of great help.
If the requirement is strictly 10 numbers in the above grouping with spaces in the middle, the regex is simple:
/^\d{4}\s\d{4}\s\d{2}$/
Where \d means that it would only match a numeric character, {4} means that it would look exactly 4 times for the previous match (\d), and in this case that would match 4 numeric characters. \s means one whitespace, and similarly like the {4}, \d{2} matches 2 numeric characters. The ^ and $ mean start of the string to be matched and end of the string to be matched respectively.
Hope this helps.
If the length is fixed then you can just use \d to represent a digit
/^\d\d\d\d \d\d\d\d \d\d\d\d \d\d$/
or use the {n} multiplier instead
/^\d{4} \d{4} \d{4} \d\d$/
if instead the total length is arbitrary and you just want to be sure that every four digits you have a space things are just slightly more complex:
/^(\d{4} )*\d{1,4}$/
the meaning is that you want zero or more groups formed with 4 digits and one space followed by 1 to 4 digits. In the last part you can use {0,4} if you also want to accept an empty string as a valid response.
If you want 1 or more of something use '+'. For example 4+ would be 1 or more consecutive '4's.
Use * to for things that you want 0 or more of!
Use parentheses for groups of characters or groups of other groups.
If you want a space in between, then use the space character between two of them.
It looks like you want 1 or more '4's followed by 0 or more (space followed by 1 or more '4's)
This regular express would match all of the following strings: "4+( 4+)*"
44444
4 44 4
4 4 4
4
4444444444
4 4
44444444444444 44444444444444444 4444444444444444
4444 4444 44
As per example provided this regex will help
/^[0-9][0-9 ]*$/
This represent numbers with spaces in between. For eg. 444 444. But if you put in this way ' 444 444' like first inserting space then start the numbers then it wont allow.
For that you can use /^[0-9 ]*$/
^ represent start and $ represent end. So between start and end you can write numbers with spaces.

Regex for limited numbers, unlimited middle dashes?

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'))

Regular expression to validate textbox length

I want a regular expression to validate an ASP textbox field with the minimum length of 11 characters and in the middle of the string should be a "-" sign. The sample string is: "0000-011111". I want to validate the textbox to make sure user enters a minimum of 10 numbers with "-" sign after 4 digits using regular expressions. Please help me.
Thank you.
Use
\d{4}-\d{6}
\d represents a digit, - is a literal dash and the number in the curly brackets force the preceeding token to be present the given number of times.
^\d{4}-\d{6,}$
You should use also ^ at the beginning and $ at the end to ensure that there is nothing before and after your string that you don't want to have. Also important is the {6,} so it will match at least 6 digits, without , it will match exactly 6 digits. If you want set a maximum of digits you can specify after the ,, e.g. {6,20}.

Categories