This might seem simple, but I have no knowledge in regex whatsoever. I need a regex that allows numbers, letters, hyphens and underscores only.. anything else is not allowed, so far I have:
/^[a-zA-Z\d]+$/
This matches numbers and letters but not hypens or underscores.
I don't want it to match any special characters within the string
Does anyone understand what I am doing wrong?
You say
a regex that allows numbers, letters, hyphens and underscores only
So, use
^[a-zA-Z0-9_-]+$
You can test it here.
Explanation:
^ - Start of string
[a-zA-Z0-9_-]* - Character class matching lowercase (a-z) and uppercase (A-Z) letters, numbers (0-9), an underscore (_), and a hyphen (-). Note the hyphen is at the end, and thus does not have to be escaped. + means 1 or more occurrences. If you allow an empty string, use * instead.
$ - End of string
Related
I need help with my regular expression written in javascript.
I have tried using the regularExpression generator online, and the best i can come up with is the this:
^[a-z.-]{0,50}$
The expression must validate the following
String first char MUST start with a-z (no alpha)
String can contain any char in range a-z (no alpha), 0-9 and the characters dash "-" and dot "."
String can be of max length 50 chars
Examples of success strings
username1
username.lastname
username-anotherstring1
this.is.also.ok
No good strings
1badusername
.verbad
-bad
also very bad has spaces
// Thanks
Almost (assuming "no alpha" means no uppercase letters)
https://regex101.com/r/O9hvLP/3
^[a-z]{1}[a-z0-9\.-]{0,49}$
The {1} is optional, I put it there for descriptive reasons
I think this should cover what you want
^[a-z][a-z0-9.-]{0,49}$
That is starts a-z but then has 0-49 of a-z, 0-9 or .-
Live example: https://regexr.com/5k8eu
Edit: Not sure if you intended to allow upper and lowercase, but if you did both character classes could add A-Z as well!
If the . and - can not be at the end, and there can not be consecutive ones, another option could be:
^[a-z](?=[a-z0-9.-]{0,49}$)[a-z0-9]*(?:[.-][a-z0-9]+)*$
Explanation
^ Start of string
[a-z] Match a single char a-z
(?=[a-z0-9.-]{0,49}$) Assert 0-49 chars to the right to the end of string
[a-z0-9]* Match optional chars a-z0-9
(?:[.-][a-z0-9]+)* Optionally match either . or - and 1+ times a char a-z0-9
$ End of string
Regex demo
I need the password to fulfill these requirements
Password must contain at least 8 word characters
Must have at least 1 numeric digit e.g. 3
Must have at least 2 uppercase characters but not in one consecutive sequence
It doesn't seems to work with this
var pos = myPass.value.search(/^([\w.-]{8,})(?=.*\d)((.*?[A-Z]){2,})$/);
No.3 is the hardest.
You are mixing consuming and non-consuming patterns adding limiting quantifiers to the consuming patterns that match a sequence, while you need to just check if a string matches some restrictive patterns or not. To add those restrictions you need lookaheads. (?=.*\d) is a correct part of your regex, other aren't.
Also, a RegExp#test() is a better method to check if a string matches or not.
Use
/^(?=\D*\d)(?=(?:(?:^|[^A-Z]+)[A-Z]){2}).{8,}$/.test(myPass.value)
See the regex demo
Or, to allow only letters, digits, underscores, dots and hyphens in the password:
/^(?=\D*\d)(?=(?:(?:^|[^A-Z]+)[A-Z]){2})[\w.-]{8,}$/.test(myPass.value)
^^^^^^
Details:
^ - start of string
(?=\D*\d) - after 0+ non-digits (\D*) at the start of the string, there must be a digit (\d) (note that after this lookahead execution, the regex index is still at the beginning of the string)
(?=(?:(?:^|[^A-Z]+)[A-Z]){2}) - there must be 2 sequences ((?:...){2}) of:
(?:^|[^A-Z]+) - start of string or one or more chars other than uppercase letters
[A-Z] - an uppercase letter.
.{8,} - any 8 or more chars other than those used in a linebreak sequence
OR
[\w.-]{8,} - 8 or more ASCII letters and digits, underscores, dots or hyphens
$ - end of string.
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)
i want to allow and start with standard English characters with numbers and 3 special chars -_. but it must contains at least one A-Za-z character
i have this so far but it still allow arabic and Chinese characters in the middle of string
/^[a-zA-Z]|\d+[-_.]$/
Try this regular expression:
/^[a-zA-Z][a-zA-Z\d-_\.]+$/
This requires the first character to be a-zA-Z, then allows letters, numbers and your special characters for any further characters.
Your RegExp /^[a-zA-Z]|\d+[-_.]$/, is checking that the first character is a letter or the last character is a number, hyphen, underscore of anything.
What you want is something more like the following (note how - and . have been escaped)
/^[a-zA-Z][a-zA-Z\d\-_\.]*$|^[a-zA-Z\d\-_\.]*[a-zA-Z]$/
i.e. String starts with a letter, then has any number of letters, numbers, hyphens, underscores or dots until the end or String ends with a letter and has any number of letters, numbers, hyphens, underscores or dots before it.
To allow [-a-zA-Z0-9_.] with a minimum of one [a-z], you just have to define [a-z] in the middle and any quantity of other chars around it.
/^[-a-zA-Z0-9_.]*[a-z][-a-zA-Z0-9_.]*$/
How can I write a regex to match strings following these rules?
1 letter followed by 4 letters or numbers, then
5 letters or numbers, then
3 letters or numbers followed by a number and one of the following signs: ! & # ?
I need to allow input as a 15-character string or as 3 groups of 5 chars separated by one space.
I'm implementing this in JavaScript.
I'm not going to write out the whole regex for you since this is homework, but here are some hints which should help you out:
Use character classes. [A-Z] matches all uppercase. [a-z] matches all lowercase. [0-9] matches numbers. You can combine them like so [A-Za-z0-9].
Use quantifiers like {n} so [A-Z]{3} gives you 3 uppercase letters.
You can put other characters in character classes. Let's say you wanted to match % or # or #, you could do [%##] which would match any of those characters.
Some meta-characters (characters which have special meaning in the context of regular expressions) will need to be escaped like so: \$ (since $ matches the end of a line)
^ and $ match the beginning and end of the line respectively.
\s matches white-space, but if you sanitize your input, you shouldn't need to use this.
Flags after the regex do special things. For example in /[a-z]/i, the i ignores case.
This should be it:
/^[a-z][a-z0-9]{4} ?[a-z0-9]{5} ?[a-z0-9]{3}[0-9][!&#?]$/i
Feel free to change 0-9 and [0-9] with \d if you see fit.
The regex is simple and readable enough. ^ and $ make sure this is a whole match, so there aren't extra characters before or after the code, and the /i flag allows upper or lower case letters.
I would start with a tutorial.
Pay attention to the quantifiers (like {N}) and character classes (like [a-zA-Z])
^[a-zA-Z][a-zA-Z0-9]{4} ?[a-zA-Z0-9]{5} ?[a-zA-Z0-9]{3}[\!\&\#\?]$