I need to match exactly 11 occurences of the same digit in a group, like:
11111111111
55555555555
But not:
11111000111
55552225555
What I've tried so far can get 11 occurences of digits:
/([0-9]){11}/g
/\d{11}/g
But it will match any 11 digits.
I've managed to do this:
/(0{11}|1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11})/g
Is there any other easier way to do it?
/(\d)\1{10}/
This matches the first digit and uses a reference to that digit \1 to match it ten more times. Note that this will also match if the digit repeats 12 or more times, and if other digits start the string, but this seems to be desired.
You should use backreference: ((\d)\2{10})
The \2 matches "the same thing as was the 2nd caputing group (parentheses)".
https://regex101.com/r/QESWrJ/1
Related
I want to check if a mobile number has 11 digits, starts with zero and 3rd digit could be 0,1,2,3 and 9 only like 09123456789. I used this pattern ^(09)([01239])\d{8}$ for this purpose, also I wanna check that 4th to 11th digits are not duplicated completely like 09123333333 or 090311111111.
I use this Regex pattern: ^(09)([01239])(?!\2{8,})\d{8}$ to achieve this purpose but unfortunately it doesn't work.
Note that I have to use one Regex pattern exactly.
Could anyone help me please?
You may use
^09[01239]\d(\d)(?!\1{6})\d{6}$
It matches
^ - start of string
09 - a 09 substring
[01239] - a digit from the set
\d - a digit
(\d) - Group 1: a digit
(?!\1{6}) - no same digit as captured into Group 1 is allowed
\d{6} - six digits
$ - end of string.
I have a numeric code which varies in length from 6-11 digits
which is separated by hyphen after each 3 digits
possible combinations
123-456
123-456-78
123-456-7890
So, here I am trying to convert the user entered code to this format even when entered with spaces and hyphens in the middle.
For Ex:
123 456-7 -> 123-456-7
123456 789 -> 123-456-789
123456 -> 123-456
Valid user input format is 3digits[space or hyphen]3digits[space or hyphen]0to5digits
I tried it like this
code.replace(/^(\d{3})[- ]?(\d{3})[- ]?(\d{0,5})$/,'$1-$2-$3');
But when there are only 6 digits there is a hyphen(-) at the end of the number which is not desired.
123-456-
Could anybody help me with this? Thank you.
The easiest way is probably to just do this with a second replace:
code.replace(/^(\d{3})[- ]?(\d{3})[- ]?(\d{0,4})$/,'$1-$2-$3')
.replace(/-$/, '');
This is chaining a second replace function, which says "replace a - at the end of the string with an empty string". Or in other words, "if the last character of the string is - then delete it.
I find this approach more intuitive than trying to fit this logic all into a replace command, but this is also possible:
code.replace(
/^(\d{3})[- ]?(\d{3})[- ]?(\d{0,4})$/,
'$1-$2' + ($3 == null ? '' : '-') + $3
)
I think it's less obvious at a glance what this code i doing, but the net result is the same. If there was no $3 matched (i.e. your sting only contained 6 digits), then do not include the final - in the replacement.
I believe this will do it for you - replace
^(\d{3})[ -]?()|(\d{3})[ -]?(\d{1,5})
with
$1$3-$2$4
It has two alternations.
^(\d{3})[ -]?() matches start of line and then captures the first group of three digits ($1), then optionally followed by a space or an hyphen. Finally it captures an empty group ($2).
(\d{3})[ -]?(\d{1,5}) matches, and captures ($3), three digits, optionally followed by a space or an hyphen. Then it matches and captures (($4)) the remaining 1-5 digits if they're present.
Since the global flag is set it will make one or two iterations for each sequence of digits. The first will match the first alternation, capturing the first three digits into group 1. Group 2 will be empty.
For the second iteration the first match have matched the first three digits, so this time the second alternation will match and capture the next three digits into group 3 and then the remaining into group 4.
Note! If there only are three digits left after the first match, none of the alternations will match, leaving the last three digits as is.
So at the first iteration group 1 are digits 123. group 2, 3 and 4 are empty. The second iteration group 1 and two are empty, group 3 are the digits 456 and group 4 are digit 7-11.
This gives the first replace $1 = 123- plus nothing, and the second 456-67....
There's no syntax checking in this though. It assumes the digits has been entered as you stated they would.
See it here at regex101.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
https://www.freecodecamp.com/challenges/find-numbers-with-regular-expressions
I was doing a lesson in FCC, and they mentioned that the digit selector \d finds one digit and adding a + (\d+) in front of the selector allows it to search for more than one digit.
I experimented with it a bit, and noticed that its the g right after the expression that searches for every number, not the +. I tried using \d+ without the g after the expression, and it only matched the first number in the string.
Basically, whether I use \d or \d+, as long as I have the g after the expression, It will find all of the numbers. So my question is, what is the difference between the two?
// Setup
var testString = "There are 3 cats but 4 dogs.";
var expression = /\d+/g;
var digitCount = testString.match(expression).length;
The g at the end means global, ie. that you want to search for all occurrences. Without it, you'll just get the first match.
\d, as you know, means a single digit. You can add quantifiers to specify whether you want to match all the following, or a certain amount of digits afterwards.
\d means a single digit
\d+ means all sequential digits
So let's say we have a string like this:
123 456
7890123
/\d/g will match [1,2,3,4,5,6,7,8,9,0,1,2,3]
/\d/ will match 1
/\d+/ will match 123
/\d+/g will match [123,456,7890123]
You could also use /\d{1,3}/g to say you want to match all occurrences where there are from 1 to 3 digits in a sequence.
Another common quantifier is the star symbol, which means 0 or more. For example /1\d*/g would match all sequences of digits that start with 1, and have 0 or more digits after it.
Counting the occurrences of \d will find the number of digits in the string.
Counting the occurrences of \d+ will find the number of integers in the string.
I.E.
123 456 789
Has 9 digits, but 3 integers.
\d means any digit from 0 to 9, the + says "one or more times".
As long as your numbers are single digit there is no difference, but in the string "I have 23 cows" and \d would match 2 alone whereas \d+ would match 23.
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/
Can anyone explain to me the differences between these two regex approaches:
/(\d)\1/
/(\d){2,}/
As far as I can see they both match for at least one recurrence of a subexpression. If they, in fact, do the same thing, are there any performance issues that distinguish them?
No they don't do the same
/(\d)\1/
matches
11 and 22 and 33
With the brackets you put the matched digit in a capture group and access that variable with \1, so you match two equal digits in a row.
while
/(\d){2,}/
matches
12 and 22 and 123456789 and 22222222
Here you say match two or more ({2,}) digits in a row. This can be different digits.
/(\d)\1/ - Match a digit, capture it in group 1, and then match the same digit again, using a back references.
/(\d){2,}/ - Match 2 digits or more. The last digit will be captured in a group. Each digit is matched independently, they don't have to be the same.