positive lookahead - javascript

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.

Related

Regex to capture all numbers in a string that ends with a number

I'm trying to write a regex that captures all the numbers in a string BUT only if the string ends with numbers.
I worked out the pattern would require a repeating capture group:
^(\D*(\d+))+$
So
the string starts
there are 0 or more non-digit characters
then 1 or more digits (which we capture)
that pattern repeats until the end of the string
My problem is that it seems that in repeated capture groups you only get the last match returned to you. (demo)
Can anyone show me where I'm going wrong?
You may use this regex with a lookahead:
\d+(?=(?:\w+\d)?\b)
RegEx Demo
RegEx Breakdown:
\d+: Match 1+ digits
(?=: Start Lookahead assertion
(?:\w+\d)?: Optionally match 1 or more word characters followed by a digit
\b: Word boundary
): End Lookahead assertion

Regex for an alphanumeric string of specific length with at least one letter and at least one digit

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)}`)
)

Javascript Regular Expresion [duplicate]

I'm trying to write a RegExp to match only 8 digits, with one optional comma maybe hidden in-between the digits.
All of these should match:
12345678
12,45678
123456,8
Right now I have:
^[0-9,]{8}
but of course that erroneously matches 012,,,67
Example:
https://regex101.com/r/dX9aS9/1
I know optionals exist but don't understand how to keep the 8 digit length applying to the comma while also keeping the comma limited to 1.
Any tips would be appreciated, thanks!
To match 8 char string that can only contain digits and an optional comma in-between, you may use
^(?=.{8}$)\d+,?\d+$
See the regex demo
The lookahead will require the string to contain 8 chars. ,? will make matching a comma optional, and the + after \d will require at least 1 digit before and after an optional comma.
If you need to match a string that has 8 digits and an optional comma, you can use
^(?:(?=.{9}$)\d+,\d+|\d{8})$
See the regex demo
Actually, the string will have 9 characters in the string (if it has a comma), or just 8 - if there are only digits.
Explanation:
^ - start of string
(?:(?=.{9}$)\d+,\d+|\d{8}) - 2 alternatives:
(?=.{9}$)\d+,\d+ - 1+ digits followed with 1 comma followed with 1+ digits, and the whole string matched should be 9 char long (8 digits and 1 comma)
| - or
\d{8} - 8 digits
$ - end of string
See the Java code demo (note that with String#matches(), the ^ and $ anchors at the start and end of the pattern are redundant and can be omitted since the pattern is anchored by default when used with this method):
List<String> strs = Arrays.asList("0123,,678", "0123456", // bad
"01234,567", "01234567" // good
);
for (String str : strs)
System.out.println(str.matches("(?:(?=.{9}$)\\d+,\\d+|\\d{8})"));
NOTE FOR LEADING/TRAILING COMMAS:
You just need to replace + (match 1 or more occurrences) quantifiers to * (match 0 or more occurrences) in the first alternative branch to allow leading/trailing commas:
^(?:(?=.{9}$)\d*,\d*|\d{8})$
See this regex demo
You can use following regex if you want to let trailing comma:
^((\d,?){8})$
Demo
Otherwise use following one:
^((\d,?){8})(?<!,)$
Demo
(?<!,) is a negative-lookbehind.
/^(?!\d{0,6},\d{0,6},\d{0,6})(?=\d[\d,]{6}\d).{8}$/
I guess this cooperation of positive and negative look-ahead does just what's asked. If you remove the start and end delimiters and set the g flag then it will try to match the pattern along decimal strings longer than 8 characters as well.
Please try http://regexr.com/3d63m
Explanation: The negative look ahead (?!\d{0,6},\d{0,6},\d{0,6}) tries not to find any commas side by side if they have 6 or less decimal characters in between while the positive look ahead (?=\d[\d,]{6}\d) tries to find 6 decimal or comma characters in between two decimal characters. And the last .{8} selects 8 characters.

Javascript regex with 20 chars, more than n numbers in a row

I'm trying to put two regex's together and haven't figured it out.
I've got the following to match only alphanumeric
^([a-zA-Z0-9_-]){1,20}$
and i've got the following to match any string with more than 7 numbers in a row
^\d{8,20}$
How do I put the two together to allow for only 20 alphanumeric characters, with more than 7 numbers in a row
You may use
/^(?=.*\d{8})[\w-]{1,20}$/
See the regex demo
Details
^ - start of string
(?=.*\d{8}) - after any 0+ chars other than line break chars there must be 8 consecutive digits
[\w-]{1,20} - 1 to 20 word chars (letters, digits or _) or hyphens
$ - end of string.
JS demo:
var ss = ["Text_with_1234567", "Text_with_12345678"];
var rx = /^(?=.*\d{8})[\w-]{1,20}$/;
for (var s of ss) {
console.log(s,"=>",rx.test(s));
}
NOTE:
If you want to make sure these 8 or more digits appear at the start of string, remove .* in the lookahead
If you want to make sure the digits are at the end, use (?=.*\d{8}$).

validation format of javascript

I found these javascript validation codes:
<script type="text/javascript">
function validasi_input(form){
pola_username=/^[a-zA-Z0-9\_\-]{6,100}$/;
if (!pola_username.test(form.username.value)){
alert ('Username minimal 6 karakter dan hanya boleh Huruf atau Angka!');
form.username.focus();
return false;
}
return (true);
}
</script>
I want to ask about this part:
pola_username=/^[a-zA-Z0-9\_\-]{6,100}$/;
does anyone can tell me how to understand this kind of format? is it format for letter, or number, or characters?
/^[a-zA-Z0-9\_\-]{6,100}$/;
In english this means: that a string can have any letter either uppercase or lowercase, numbers, underscores, and hyphens. A minimum length of 6 characters, and a maximum length of 100.
Further details:
The string must start with either a letter, number, underscore, or hyphen.
/^[a-zA-Z0-9\_\-]{6,100}$/
^ asserts that we are at the beginning of the string
[a-zA-Z0-9_-] string can have any letter either uppercase , lowercase, numbers, underscores, or hyphens.
{6,100} matches a length of character having from 6 to 100
$ asserts that we are at the end of the string
Various RegEx explanation/testing tools.
1. Explain RegEx
2. RegEx101
3. Debuggex Demo
^[a-zA-Z0-9\_\-]{6,100}$
^ is an anchor. It asserts position at start of the string
[a-zA-Z0-9\_\-]{6,100} match a single character present in the list below
{6, 100}: Between 6 and 100 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)
0-9 a single character in the range between 0 and 9
\_ matches the character _ literally
\- matches the character - literally
$ is an anchor. It asserts position at end of the string.
An alternative regex using flags would be:
/^[a-z\d\_\-]{6,100}$/i
Here \d matches digits (0-9), and flag i denotes case insensitivity.
This is what regular expressions do to perform matches, for starters:
(source: gyazo.com)

Categories

Resources