difference between 2 regex strings [duplicate] - regex

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
What is the difference in what these 2 are doing? Thx
var m = document.referrer.match(/\&cd=([\d]*)/);
and
var m = document.referrer.match(/cd=(.*?)&/);
Which one is more efficient and effective?

/\&cd=([\d]*)/ - matches any string starting with an "&cd=" followed by any zero or more decimal digits. The first capture group is the decimal digits.
/cd=(.*)&/ - matches any string starting with an "cd=" followed by zero or more characters up to and including the first "&". The first capture group is all characters between "cd=" and "&".
They are similar, but not equivalent. Which one you should use depends on your exact needs. Judging from your comment, it sounds like you want to use:
var m = document.referrer.match(/[?&]cd=(\d+)/);

Related

Regex: how to find two letters that are repeating at least 3 number of times? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
For example, I would like to find ty in:
erytypotym5ty
etytyty
koetymitywty
or il in:
keililmil
ilwrilltyil5ile
^.*(\w{2}).*\1.*\1.*$.
The two letters (also digits and _; you could replace \w with [a-zA-Z], if you don't want them) will be in group 1.
https://regex101.com/r/11Oq70/1

Regular expression to match exact 11 digits numbers [duplicate]

This question already has answers here:
regular expression to match exactly 5 digits
(5 answers)
RegEx match exactly 4 digits
(4 answers)
Closed 2 years ago.
I need a regular expression to match exact 11 digits(no spaces, no alphabets). Valid values are
12345678987 - Valid
123456789876 - not valid because it has more than 11 digits
1234 5678987 - Not valid (since it has space)
11223344556 - valid
A1234B5678987 - not valid (because it has alphabets)
I tried the below expression but no luck
var aa = new RegExp("^[0-9]{11});
var bb = new RegExp("/d{11});( this will give valid to even 1,2,3,4)
Use start and end of string
^\d{11}$
const str = `12345678987
123456789876
1234 5678987
11223344556
A1234B5678987`;
console.log(str.match(/^\d{11}$/gm))
or using a Word Boundary:
\b\d{11}\b

Regex Help - Named capture group that ends in two possible ways [duplicate]

This question already has answers here:
How to extract a value from a URL query string in C#?
(4 answers)
Closed 4 years ago.
I'm trying to capture a group that has a variety of different forms.
cardType=A&Return=True
cardType=AbC321
Return=False&cardType=C
My current regex is:
cardType=(?<Card Type>.*)&?
This currently captures the 2 and 3, but not in 1 as it also captures Return in that case.
If I do instead:
cardType=(?<Card Type>.*?)&
Then it correctly captures 1, but not 2 and 3.
How do I write a regex that captures it in all 3 cases?
Use:
cardType=(?<CardType>[^&\s]*)
Demo
Two important changes:
removed space form group name
replaced .*)&? with [^&\s]*)
The second change is more important. When you do .*&?, then &? is never captured, because .* takes the & sign. As in most cases it's better to limit the repetition by limiting scope of accepted charactes to [^&\s] - anything but whitespace or ampersand

Regular Expression for US and other format zip [duplicate]

This question already has answers here:
What is the ultimate postal code and zip regex?
(20 answers)
Closed 9 years ago.
Am working on regular expression for following formats of zip in groovy
Includes a letter (L12345)
Includes a dash plus 4 more numbers (77056-1234)
Includes spaces (77056 1234)
I have this "^\d{5}(-\d{4})?\$" but its not matching the required formats. Could anyone please help me?
^\d{5}(?:[-\s]\d{4})?$
^ = Start of the string.
\d{5} = Match 5 digits (for condition 1, 2, 3)
(?:…) = Grouping
[-\s] = Match a space (for condition 3) or a hyphen (for condition 2)
\d{4} = Match 4 digits (for condition 2, 3)
…? = The pattern before it is optional (for condition 1)
$ = End of the string.
This is from the following question, hope it helps
regex for zip-code
For the optiona startingil letter use
[A-Z]?
to make the letter optional. {1} is redundant. (Of course you could also write [A-Z]{0,1} which would mean the same, but that's what the ? is there for.)
I think it should go after the ^ but haven't had a chance to test

regex: match at least one character from set [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I have a character set {x, y, z} and I want to check if some string contains at least one character from this set.
For example:
abxyz - valid
zabc1 - valid
abc4e - not valid
/.*[xyz].*/ should do the trick
Try this regex:
.*[x-z].*
This will only match lines that include [x-z] at least once.
Example