I want to write a regular expression in javascript to match the following valid scenarios. later on, If I have to extract using a group then I will be able to do so.
I have tried the below regex, but it not giving me the desired pattern.
/[0-9][+-[0-9]+]*/
Regex should be able to match the "Valid" scenarios.
// Valid
64344
9434
434-543
434-543
434 - 543
4435 - 343434
1-2,3,5,6
1-2,3,5,6-6,45-4
3,5,6, 8 - 9,24
1-2,3,5 ,6
1- 2,3, 5,6-6,45-4
// Invalid
23 -
2343.3434
343.3434 - 13.466
23 ,
234,
,54
xyz 9
3,5,6,8-9,24,
,35,65,65
,35,65,65 -
,3 5,65,65,
3,5,6, 8- 9,24,
1- 2,3, 5,6-6,4 5-4
, 35,65,6 5
,35, 6 5,65,
Looking at your samples, I feel what you want is a comma separated numbers where some of which can be ranged like 1-2 and also having optional spaces between them and also at start or end of string. Here is a regex you can use,
^\s*\d+(?:\s*-\s*\d+\s*)?(?:\s*,\s*\d+(?:\s*-\s*\d+)?)*\s*$
Explanation:
^ - Start of string
\s* - optional space at start of input
\d+ - Matches one or more just number
(?:\s*-\s*\d+\s*)? - This matches a ranged number like this -2 which can have spaces within it and ? at the end means ranged part is optional
(?:\s*,\s*\d+(?:\s*-\s*\d+)?)* - This regex part ensures that the numbers can be comma separated and \d+(?:\s*-\s*\d+)? part in it enables numbers of 2-3 form where ? in it indicates that it can be just pure number without having ranged part and * whole of this can be zero or more times
\s* - optional space at the end of input
$ - end of input
Live Demo
Related
I have strings similar to; First Half Goals 1.5/2. The text at the beginning can be anything so cannot depend on that as part of the RegEx as it varies. What I want to do is to match the number sequence at the end. Test samples would be:
5
0.5/1
2.5/3
147
The sequence can either contain a slash or it cannot. If it contains a slash, there must be a value after it - either integer or decimal. The value before the decimal (if one is present) can be any number of digits - \d+ - but the value after should must be 0 or 5 - (0|5). The 1st value before the slash (/) is either an integer or a decimal. If the sequence contains a slash, then the number after is also either a integer or a decimal. All values are positive.
The main point of this RegEx is that I need it to only match once. The was the RegEx I was using:
(\d(\.(0|5))?\/\d(\.(0|5)))|(\d\.(0|5))|(\d)
The issue with the regex above is that the example string I use; First Half Goals 1.5/2 matched twice. Once on the 1.5 & the 2nd on the 2. I have now altered it to be this:
\d+(\.(0|5))?(\/?\d+(\.(0|5))?)?
This is slightly better but if I give the test sample; 1.6/2, it will match 6/2. This will be because the decimal section on the former number is optional. I'm not sure if a lookbehind would come in handy here, I don't have much experience with them. Sadly the text beforehand is so unpredictable otherwise I could trim the string to only get the wanted substring & then match from the start of the string but can't do that. An outline of what should match & what should not:
1 // Match
5.5 // Match
7.8 // No Match
0/0.5 // Match
147/147.5 // Match
2. // No Match
6.5/ // No Match
7.0/8 // Match
10.0 // Match
1./2.5 // No Match
5./6 // No Match
/157 // No Match
/4.5 // No Match
I've tried to explain as best I could but if you need more details then I'll provide them
In Node.js, RegExp supports lookbehinds and you may use
/\b(?<!\d\.|\/)\d+(?:\.[05])?(?:\/\d+(?:\.[05])?)?$/
See the regex demo
Details
\b - word boundary
(?<!\d\.|\/) - no digit + dot or a slash are allowed immediately to the left of the current location
\d+ - one or more digits
(?:\.[05])? - an optional sequence of
\. - a dot
[05] - 0 or 5
(?:\/\d+(?:\.[05])?)? - an optional sequence of
\/\d+ - a / and 1+ digits
(?:\.[05])? - an optional sequence of a dot and then 0 or 5
$ - end of string.
I am trying to implement input number field which will not allow if number end with two 0's i.e. if I enter 23100 then it should not allow it.
I'm using the regex /^[0-9]*00$/ but it allowing 123100.
I have pasted my code
enter code here
You may use
/^(?!\d*00$)\d+$/
It will match
^ - start of string
(?!\d*00$) - a negative lookahead that makes sure no 0+ digits followed with 00 at the end are allowed
\d+ - one or more digits
$ - end of string
Updated now to now use look-behinds or look-aheads.
^\d*(?:\d?[1-9]|[1-9]\d)$
Demo
try this.
(^\d+[1-9]+0{0,1}$)
This will work for numbers like
12
123
12310
etc
any number with 2 or more 0's at the end doesn't match in this case
Hi I don't know much about regular expression. But I need it in form validation using angularJs.
Below is the requirement
The input box should accept only if either
(1) first 2 letters alpha + 6 numeric
or
(2) 8 numeric
Below are some correct Inputs :-
(1)SH123456
(2)12345678
(3)sd456565
I tried data-ng-pattern="/(^([a-zA-Z]){2}([0-9]){6})|([0-9]*)?$/" , Its working fine for both the above condition but still it is accepting strings like S2D3E4F5 and may be many other combination as well.
What I am doing wrong I am not able to find it out.
Any help is appreciable !!!
Thanks
In your regex, the two alternative branches are anchored separately:
(^([a-zA-Z]){2}([0-9]){6}) - 2 letters and 6 digits at the start of the string
| - or
([0-9]*)?$ - optional zero or more digits at the end of the string
You need to adjust the boundaries of the group:
data-ng-pattern="/^([a-zA-Z]{2}[0-9]{6}|[0-9]{8})?$/"
^ ^^^^
See the regex demo.
Now, the pattern will match:
^ - start of string
( - start of the grouping:
[a-zA-Z]{2}[0-9]{6} - 2 letters and 6 digits
| - or
[0-9]{8} - 8 digits
)? - end of the grouping and ? quantifier makes it match 1 or 0 times (optional)
$ - end of string.
You can try this DEMO LINK HERE
^(([a-zA-Z]{2}|[0-9]{2})[0-9]{6})?$
It will accept:
ab123456
12345678
aa441236
aw222222
I am trying to write a regular expression for a bit of javascript code that takes a user's input of a mobile number and in one regular expression, performs the following checks:
Starts with 07
Contains only numbers, whitespace or dashes
Contains exactly 11 numbers
Is this possible to do in just one regular expression and if so, how please?
I don't think it is possible with one regex, but it is possible by testing for two conditions:
if(/^07[\d\- ]+$/.test(str) && str.replace(/[^\d]/g, "").length === 11) {
//string matches conditions
}
Explanation of the regex:
^: Anchor that means "match start of string".
07: Match the string 07. Together with the above, it means that the string must start with 07.
[: Beginning of a character class i.e., a set of characters that we want to allow
\d: Match a digit (equivalent to 0-9).
\-:
" ": Match whitespace (markdown doesn't let me show a single space as code)
]: End of character class.
+: One or more of the previous.
$: Anchor that means "match end of string". Together with the ^, this basically means that this regex must apply to the entire string.
So here we check to see that the string matches the general format (starts with 07 and contains only digits, dashes or spaces) and we also make sure that we have 11 numbers in total inside the string. We do this by getting of anything that is not a digit and then checking to see that the length of the string is equal to 11.
Since #Vivin throws out the challenge :
/^07([-\s]*\d){9}[-\s]*$/
^07 : begin with digits 07
( : start group
[-\s]* : any number of - or whitespace
\d : exactly one digit
){9} : exactly 9 copies of this group (11 digits including 07)
[-\s]* : optional trailing spaces or -
$ : end of string
Of course a more useful way might be as follows
if ((telNo = telNo.replace (/[-\s]+/g, '')).match (/^07\d{9}$/)) {
....
}
which has the advantage (?) of leaving just the digits in telNo
Thank you all for trying, but after a good while trying different ideas, I finally found a working "single" regular expression:
07((?:\s|-)*\d(?:\s|-)*){9}
This make sure that it starts with 07, only contains digits, whitespace or dashes, and only 11 of them (9 plus the first 2) are numbers.
Sorry to have wasted your time.
Explanation:
() - include in capture
(?:) - do not include in capture
\s - whitespace
| - or
- - dash
* - zero or more
\d - digits only
{9} - exactly nine of what is captured
I need a regular expression that validate for a number with length 4, 5, 6
I used ^[0-9]{4} to validate for a number of 4, but I do not know how to include validation for 5 and 6.
Try this:
^[0-9]{4,6}$
{4,6} = between 4 and 6 characters, inclusive.
[0-9]{4,6} can be shortened to \d{4,6}
Be aware that, as written, Peter's solution will "accept" 0000. If you want to validate numbers between 1000 and 999999, then that is another problem :-)
^[1-9][0-9]{3,5}$
for example will block inserting 0 at the beginning of the string.
If you want to accept 0 padding, but only up to a lengh of 6, so that 001000 is valid, then it becomes more complex. If we use look-ahead then we can write something like
^(?=[0-9]{4,6}$)0*[1-9][0-9]{3,}$
This first checks if the string is long 4-6 (?=[0-9]{4,6}$), then skips the 0s 0*and search for a non-zero [1-9] followed by at least 3 digits [0-9]{3,}.
If the language you use accepts {}, you can use [0-9]{4,6}.
If not, you'll have to use [0-9][0-9][0-9][0-9][0-9]?[0-9]?.
To match standalone 4-6-digit numbers, you may use
^\d{4,6}$ // If full string match is expected
\b\d{4,6}\b // If no letters/digits/underscores are expected on both ends
(?<!\d)\d{4,6}(?!\d) // If no digits are expected on both ends, but letters/_ are allowed
(^|\D)(\d{4,6})(?!\d) // Same as above, in case lookbehinds are not supported (get Group 2 value)
See Regex #1 - Regex #2 - Regex #3 and Regex #4 demos.
Details:
^ - start of string
\b - a word boundary
(?<!\d) - a negative lookbehind that fails the match if there is a digit immediately to the left of the current location
(^|\D) - a capturing group matching either start of string or a non-digit char
\d{4,6} - four, five or six digits
(?!\d) - a negative lookahead that fails the match if there is a digit immediately to the right of the current location
$ - end of string