I'm having issues with understanding pattern matching. I've figured it out to a point but I'm having a lot of trouble with this specific pattern I need to match.
Requirements: Check for the format of the user name. It should start with a letter followed by letters, numbers, and periods. A user name should be at least 6 characters long but no more than 12.
This is what I have so far:
var user = document.getElementById('username').value;
var pos = user.search(/^[A-z](?=.*?[A-z])(?=.*?[0-9])\./);
Try this pattern
var pattern = /^[A-Za-z][A-Za-z.0-9]{5,11}$/
^ matches beginning of file.
[A-Za-z] = matches first letter
[A-Za-z.0-9] = Matches a-z both A-Z and a-z the . (dot) character and numbers 0-9
{5,11} = tells that the last character "group" should have between 5 or 11 occurrences. witch makes the total string between 6 and 12 characters long.
$ = matches end of string
Hope this helps!
Edit:
Javascript to do the match
var pattern = /^[A-Za-z][A-Za-z.0-9]{5,11}$/
//assuming the var username contains the username
if(username.match(pattern)){
alert("Valid pattern");
}
else{
alert("Invalid pattern")
}
It should start with a letter followed by letters, numbers, and periods. A user name should be at least 6 characters long but no more than 12.
So you want to allow a string that starts with a letter and has at least one dot or number and is between 5 and 12 characters.
In that case, use the following:
^[A-Za-z](?=.*\d.*)(?=.*[.].*)[A-Za-z\d.]{5,11}$
^
match start. [A-Za-z]
a letter. (?=.*\\d.*)
positive lookahead - the following string contains at least 1 digit. (?=.*[.].*)
positive lookahead - the following string contains at least 1 dot. [A-Za-z\\d.]{5,11}
the rest of the string contains between 5 and 11 letters, digits and dots (the total string between 6 and 12). $
match end. Note: -
?=
the forward lookup does not affect the match position, so the match will continue at the second character. ?=
an AND is implied between the forward lookups, so both need to be satisfied. ^[a-zA-Z][a-zA-Z.]{5,11}$
这个应该适合你,因为你没有任何关于任何角色最小出现的约束。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.