This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 3 years ago.
Im trying to add a length requirement to the below code.
Code: ^[a-zA-z0-9\!\$\%\&]+(?: [a-zA-z0-9\!\$\%\&]+)*$
I want a sentence with spaces to only have around 1 to 10 characters. I want to count spaces as well. The code provided doesn't allow leading or trailing space but space between.
A sentence limited to 10 chars is obscenely small but you can use:
^(?=^.{1,10}$)[a-zA-z0-9\!\$\%\&]+(?: [a-zA-z0-9\!\$\%\&]+)*$
(?=^.{1,10}$) = ensure that between 1 and 10 chars exist between the start and end of the string.
https://regex101.com/r/zsa1N9/1
I see your new comment about Javascript. You would be better off adding a check for .length in addition to your regex.
Related
This question already has answers here:
How to match all numerical characters and some single characters using regex
(3 answers)
Closed 2 years ago.
I have an essay as a big string with letters and numbers (as page numbers) (only single digit and double digits). I want all of them to be replaced by "#". I tried str.replace(/[0-9]/g,"#"), this worked for 0 to 9 numbers, but with the double digits, they were replaced by "##", so how can I replace them all with a single "#"?
If you want to replace only 1 or 2 digits in a line:
str.replace(/[0-9]{1,2}/, "#")
If you want to replace all digits in a line:
str.replace(/[0-9]+/g, "#")
This question already has answers here:
Phone number validation with plus sign optional
(2 answers)
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 2 years ago.
I want to create regex for use in JavaScript where minimum 10 digit number is only allowed and maximum 15 digit. It may optionally have "+" at the start only and not at any other places.
You can use the following expression
^\+?\d{10,15}$
See a demo on regex101.com.
I believe this can do the work
var patt = new RegExp("^([+]([0-9]){10,15}$)|^(([0-9]){10,15})$");
This question already has answers here:
REGEX - Allow Numbers and . - /
(7 answers)
Closed 2 years ago.
Currently I have a textbox that accepts only Alphabet, hyphens(-), space and apostrophes('). Now I would like to add numeric values as well to it. Currently I am using the Regex as below:
/^[a-zA-ZÀ-ÖØ-öø-ÿ' -]+$/
How would I achieve adding numerics as well to the above?
To allow numbers, simply add 0-9 in your regex, like so:
/^[0-9a-zA-ZÀ-ÖØ-öø-ÿ' -]+$/
This question already has answers here:
RegExp range of number (1 to 36)
(8 answers)
Closed 6 years ago.
I'm looking for a way to validate a phone number with the length of 7, and within the rage of 8600000–9999999 (This is a region in New Zealand, witch is why google was no help as no one talks about New Zealand -- ever). Does anyone know a regEx that can do this?
I'm new to JS, but I think that a Regular Expression would be the best way to do this. (If not let me know.)
Note: The input data type of the phone number is text. (I know I can use number but I ran into bugs with it.)
EDIT -- no lines / separation, just numbers. (Also very fast answers just wow)
Thanks in advance !
This one's actually pretty easy:
/8[6-9][0-9]{5}|9[0-9]{6}/
As long as you don't need separators, anyways.
It's also pretty simple to follow:
8 matches the literal character 8.
[6-9] matches any single character that is in the ASCII range between the characters 6 and 9 (inclusive). This means that it will match any single 6, 7, 8, or 9.
Likewise, [0-9] matches any single character that is in the ASCII range betwen the characters 0 and 9. This is synonymous with "any single digit".
{5} means "match the preceding token 5 times". In this case, it's applied to [0-9], meaning it'll match 5 digits in a row.
The | (pipe) character in a regex is an alternation - it means "match either the pattern on the right or the left". This is how the regex handles the two different cases - 8600000-8999999 is handled by the pattern on the left, while 9000000-9999999 is handled by the pattern on the right.
You can use the regular expression /(?:9[0-9]|8[6-9])[0-9]{5}/.
You can click to play with it on regex101.com, and see some test cases.
Here's the breakdown (provided by regex101.com):
(?:9[0-9]|8[6-9]) Non-capturing group
1st Alternative: 9[0-9], left of |
9 matches the character 9 literally
[0-9] match a single character present in the list below
0-9 a single character in the range between 0 and 9
2nd Alternative: 8[6-9], right of |
8 matches the character 8 literally
[6-9] match a single character present in the list below
6-9 a single character in the range between 6 and 9
[0-9]{5} match a single character present in the list below
0-9 a single character in the range between 0 and 9
{5} exactly 5 times
This question already has answers here:
JavaScript regex for alphanumeric string with length of 3-5 chars
(3 answers)
Closed 7 years ago.
I need REGEXP for alpha Numeric zip code, which contains minimum 3 & maximum 10 values.
Invalid inputs are: AAAAA, A1, AA, 12, 2A
Valid inputs are: 123456, 123, A1234, A12, A12A, A9A
This is the regex I'm currently using:
/(^[A-z0-9]\d{3,10})+$/
It doesn't allow to specify only digits like this 12345 but input like A123 matches correctly.
The question is not completely clear. If you mean that you can use between 3 and 10 characters, and these characters can be alphanumerical characters (digits and [A-Za-z]), you can use:
/^(?=.*\d.*)[A-Za-z0-9]{3,10}$/
regex101 demo.
The regex works as follows:
^[A-Za-z0-9]{3,10}$ says the regex consists out of 3 to 10 characters that can be digits and/or A-Z/a-z.
The lookahead (?=.*\d.*) enfoces that the string contains at least one digit somewhere in the string.