I am looking for a regex that exactly match the text 'PDR' or 'pdr' and 8 digit so altogether 11 digit ,( 3 text + 8 digit)
pdr16120008 - TRUE
PDR16120009 -TRUE
rdp16120001- FALSE
Regex101
^(pdr|PDR)\d{8}$
Debuggex Demo
Description
^ asserts position at start of a line
1st Capturing Group (pdr|PDR)
1st Alternative pdr
pdr matches the characters pdr literally (case sensitive)
2nd Alternative PDR
PDR matches the characters PDR literally (case sensitive)
\d{8} matches a digit (equal to [0-9])
{8} Quantifier — Matches exactly 8 times
$ asserts position at the end of a line
The regex isn't difficult:
match pdr litterally at the start using ^ anchor
match exactly 8 digits using a character class and a quantifier
use the i modifier in order to match as case insensitive.
const REGEX = /^pdr[0-9]{8}$/i;
let valids = ['pdr16120008', 'PDR16120009', 'rdp16120001']
.filter(input => REGEX.test(input))
;
console.log({valids});
Related
I want to match alphanumeric string of specific length with at least one letter and at least one digit.
For example, adfyg432 should contain alphabetic and digit and the length should start from 8.
I used this expression but it won't work:
^([A-Za-z]{1,}\d{1,}){8,}$
Your current pattern repeats a group 8 or more times. That group by itself matches 1 or more chars a-z followed by 1 or more digits.
That means that the minimum string length to match is 16 chars in pairs of at least 2. So for example a string like a1aa1a1a1a1a1a1a1 would match.
You could write the pattern using 2 lookahead assertions to assert a length of at least 8 and assert at least a char a-z.
Then match at least a digit. Using a case insensitive match:
^(?=[a-z\d]{8,}$)(?=\d*[a-z])[a-z]*\d[a-z\d]*$
In parts, the pattern matches:
^ Start of string
(?=[a-z\d]{8,}$) Positive lookahead, assert 8 or more chars a-z or digits till end of string
(?=\d*[a-z]) Positive lookahead to assert at least a char a-z
[a-z]* Match optional chars a-z
\d Match at least a single digit
[a-z\d]* Match optional chars a-z or digits
$ End of string
Regex demo
const regex = /^(?=[a-z\d]{8,}$)(?=\d*[a-z])[a-z]*\d[a-z\d]*$/i;
[
"AdfhGg432",
"Abc1aaa"
].forEach(s =>
console.log(`Match "${s}": ${regex.test(s)}`)
)
Use lookaheads to match a string that is greater than 5 characters long and have two consecutive digits.
I know the solution should be
/(?=\w{6,})(?=\D*\d{2})/
But why the second element is
(?=\D*\d{2})
Instead of
(?=\d{2})
Please help me to understand this.
Actually, /(?=\w{6,})(?=\D*\d{2})/ does not ensure there will be a match in a string with 2 consecutive digits.
Check this demo:
var reg = /(?=\w{6,})(?=\D*\d{2})/;
console.log(reg.test("Matches are found 12."))
console.log(reg.test("Matches are not found 1 here 12."))
This happens because \D* only matches any non-digit chars, and once the \w{6,} matches, (?=\D*\d{2}) wants to find the two digits after any 0+ digits, but it is not the case in the string.
So, (?=\w{6,})(?=\D*\d{2}) matches a location in the string that is immediately followed with 6 or more word chars and any 0+ non-digit chars followed with 2 digits.
The correct regex to validate if a string contains 6 or more word chars and two consecutive digits anywhere in the string is
var reg = /^(?=.*\w{6,})(?=.*\d{2})/;
Or, to support multiline strings:
var reg = /^(?=[^]*\w{6,})(?=[^]*\d{2})/;
where [^] matches any char. Also, [^] can be replaced with [\s\S] / [\d\D] or [\w\W].
And to match a string that is greater than 5 characters long and have two consecutive digits you may use
var reg = /^(?=.*\d{2}).{5,}$/
var reg = /^(?=[\s\S]*\d{2})[\s\S]{5,}$/
where
^ - start of string
(?=[\s\S]*\d{2}) - there must be two digits anywhere after 0+ chars to the right of the current location
[\s\S]{5,} - five or more chars
$ - end of string.
The lookahead has to allow the 2 digits anywhere in the input. If you used just (?=\d{2}) then the 2 digits would have to be at the beginning.
You could also use (?=.*\d{2}). The point is that \d{2} has to be preceded by something that can match the rest of the input before the digits.
My regular expression should match if there aren't any consecutive letters that are the same.
for example :
"ploplir" should match
"ploppir" should not match
so I use this regular expression:
/([.])\1{1,}/
But It does the exact contrary of what I want. How can I make the match work correctly?
Code
See regex in use here
\b(?!\w*(\w)\1)\w+\b
var r = /\b(?!\w*(\w)\1)\w+\b/g
var s = "ploplir ploppir"
console.log(s.match(r))
Explanation
\b Assert position as a word boundary
(?!\w*(\w)\1\w*) Negative lookahead ensuring what follows doesn't match
\w* Match any number of word characters
(\w) Capture a word character into capture group 1
\1 Match the same text as most recently matched by the 1st capture group
\w+ Match one or more word characters
\b Assert position as a word boundary
Maybe you could use lookarounds to check if there are no consecutive letters in the string:
^(?!.*(.)(?=\1)).*$
Explanation
From the beginning of the string ^
A negative look ahead (?!
Which asserts that following .* a character (.) is not followed by the same character (?=\1) using the group reference \1
Close the negative lookahead
Match zero or more characters .*
The end of the string
Could any one please provide the regular expression for below conditions
User-part does not begin or end with a “.” Character.
Domain-part does not contain the following invalid characters: "#\"%|<>?'`/
,*&;:£$^!~ ú(){}+"
Valid email has ‘#’ and ‘.’ Character
Valid email does not contains two consecutive dots
Currently I am using the below reg ex
/^(?!^[.])[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]{2,63})*$/;
The mentioned regular expression is not working all points.
To respect all these conditions you need to treat them in order and take a logic where you write your regex in parts, I tried to make a suitable solution for your case and this is the Regex I came up with:
(?!^[.])[a-zA-Z_\-0-9]+[a-zA-Z_\-0-9.]+(?!\.\.)[^\.]+#(?![#\"%|<>?'`\/ ,*&;:£$^!~ ú(){}+])(?![\.]{2})[a-zA-Z_\-0-9.]+\.[a-zA-Z]{2,3}
Demo:
You can test this Regex here and see it in a working live Demo too.
Explanation:
Negative Lookahead (?!^[.])
Assert that the Regex below does not match ^ asserts position at start
of the string Match a single character present in the list below [.] .
matches the character . literally (case sensitive)
Match a single character present in the list below [a-zA-Z_\-0-9]+
I added this part to avoid matching the . in the beginning of the
regex
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) a-z a single character in
the range between a and z (case sensitive), A-Z
a single character in the range between A and Z
_ matches the character _ literally (case sensitive) - matches the character - literally (case sensitive) 0-9 a single character in the
range between 0 and 9
Match a single character present in the list below [a-zA-Z_\-0-9.]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) a-z a single character in
the range between a and z (case sensitive), A-Z
a single character in the range between A and Z
_ matches the character _ literally (case sensitive) - matches the character - literally (case sensitive) 0-9 a single character in the
range between 0 and 9, . matches the character . literally (case sensitive)
Negative Lookahead (?!\.\.)
Assert that the Regex below does not match \. matches the character .
literally (case sensitive) \. matches the character . literally
Match a single character not present in the list below [^\.]+
+ Quantifier — Matches between one and unlimited times, as many
times as possible, giving back as needed (greedy) \. matches the
character . literally
# matches the character # literally
Negative Lookahead (?![#\"%|<>?'\/ ,*&;:£$^!~ ú(){}+])
Assert that the Regex below does not match Match a single character
present in the list below [#\"%|<>?'/ ,*&;:£$^!~ ú(){}+]`
Negative Lookahead (?![\.]{2})
Assert that the Regex below does not match Match a single character
present in the list below [\.]{2}, {2} Quantifier — Matches exactly 2
times \. matches the character . literally
Match a single character present in the list below [a-zA-Z_\-0-9.]+
+ Quantifier — Matches between one and unlimited times, as many
times as possible, giving back as needed a-z a single
character in the range between a and z (case
sensitive), A-Z a single character in the range between A
and Z (case sensitive)
_ matches the character _ literally (case sensitive) - matches the character - literally (case sensitive) 0-9 a single character in the
range between 0 and 9, . matches
the character . literally (case sensitive) \. matches the character .
literally
Match a single character present in the list below [a-zA-Z]{2,3}
{2,3} Quantifier — Matches between 2 and 3 times, as many times as
possible, giving back as needed a-z a single character in the
range between a and z (case sensitive), A-Z a
single character in the range between A and Z
var component = {
input : $('input[name="email"]'),
mensage : {
fields : $('.msg'),
success : $('.success'),
error : $('.error')
}
},
regex = /^[a-z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)?#[a-z][a-zA-Z-0-9]*\.[a-z]+(\.[a-z]+)?$/;
component.input.blur(function () {
component.mensage.fields.hide();
regex.test(component.input.val()) ? component.mensage.success.show() : component.mensage.error.show();
});
.msg {
display: none;
}
.error {
color: red;
}
.success {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="email" id="email">Hey!</label>
<input id="email" name="email" type="email" class="required" />
<span class="msg error">You shall not pass!</span>
<span class="msg success">You can pass!</span>
Hope this helps!
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
Please explain me the meaning of the following regular expression in JavaScript with proper exploration:
/^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/
This is the meaning.
/^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/
1st Alternative: ^\b_((?:__|[\s\S])+?)_\b
^ assert position at start of the string
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
_ matches the character _ literally
1st Capturing group ((?:__|[\s\S])+?)
(?:__|[\s\S])+? Non-capturing group
Quantifier: +? Between one and unlimited times, as few times as possible, expanding as needed [lazy]
1st Alternative: __
__ matches the characters __ literally
2nd Alternative: [\s\S]
[\s\S] match a single character present in the list below
\s match any white space character [\r\n\t\f ]
\S match any non-white space character [^\r\n\t\f ]
_ matches the character _ literally
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
2nd Alternative: ^\*((?:\*\*|[\s\S])+?)\*(?!\*)
^ assert position at start of the string
\* matches the character * literally
2nd Capturing group ((?:\*\*|[\s\S])+?)
(?:\*\*|[\s\S])+? Non-capturing group
Quantifier: +? Between one and unlimited times, as few times as possible, expanding as needed [lazy]
1st Alternative: \*\*
\* matches the character * literally
\* matches the character * literally
2nd Alternative: [\s\S]
[\s\S] match a single character present in the list below
\s match any white space character [\r\n\t\f ]
\S match any non-white space character [^\r\n\t\f ]
\* matches the character * literally
(?!\*) Negative Lookahead - Assert that it is impossible to match the regex below
\* matches the character * literally
Well, in a really nice form:
You can check this out at Regex 101.