1

I'm using an online regex checker such as regex101 to check my regex which is to be used by javascript such as (working but cut down regex for example only)

state = /^[a-zA-Z0-9]$/.test($(control).val())

My Regex is

(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9].{5,19}

Which when cut down into chunks should hopefully mean...

(?=.*[A-Z])  - Must include an Upper case char
(?=.*[0-9])  - Must include a numeric 
[a-zA-Z0-9. ]  - Can include any lower  or upper case char, or Numeric, a period or space
.            - Matching previous
{5,19}       - the string must be 6-20 characters in length

This however still allows special characters such as !.

I've not used \d for decimals as I believe [0-9] should be more strict in this regard, and removed the period and space to see whether this was the cause, to no avail.

Where have I gone wrong to be allowing special characters?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Fetchez la vache
  • 4,940
  • 4
  • 36
  • 55
  • 3
    Remove the `.` in the 'Matching' previous line, the `.` matches any single character except new line, which is matching the `!` - try this - `(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{5,19}`. also \d is exactly the same as [0-9] – Mark Walters Dec 19 '16 at 16:58
  • 1
    `.` matches any character, not "previous". If you're actually using regex101.com: https://regex101.com/r/BUMWzO/1, it will tell you that: ".{5,19} matches any character (except for line terminators)" – Heretic Monkey Dec 19 '16 at 16:59
  • Cheers chaps for the quick replies. @MarkWalters as you were just first please free to post the answer for the points. Thanks again. Not sure where I got that dot idea from! – Fetchez la vache Dec 19 '16 at 17:01
  • @MikeMcCaughan - I hadn't even spotted that. I guess there's "using it" and "using it properly". Thanks :) – Fetchez la vache Dec 20 '16 at 08:03

1 Answers1

2

You need to remove the last . which you think is matching previous, it actually matches any character except for newline, so this is where the ! is getting through.

So (?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9].{5,19} should be (?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{5,19}

Also just thought i'd mention there is no difference between \d and [0-9] whatsoever.

UPDATED - this following should fix the issues you were seeing with the regex - (?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9\.\s]{6,20}$

  • Added \. (to allow a .)
  • Added \s (to allow whitespace characters)
  • Changed {5, 19} to {6, 20}$ to ensure the correct character match

If you want to test this version of the regex in regex101 here

Mark Walters
  • 12,060
  • 6
  • 33
  • 48
  • 1
    There is, actually, a difference between `\d` and `[0-9]`!. `\d` is slower, since it also takes into account numbers in other languages. [Here](http://stackoverflow.com/a/16621778/1197418) is a good explanation. – juanlu Dec 20 '16 at 09:34
  • @juanlu Actually just read this - http://stackoverflow.com/questions/16662946/d-only-matchs-0-9-digits. Unicode regex matching is not supported in JavaScript so \d is equivalent to [0-9] in JS. – Mark Walters Dec 20 '16 at 10:49
  • Interestingly, after testing more, your answer it only seems to check the first 5 characters. A1CDE matches, but so does A1CDE!, although A1C!DE fails as it's only matching the first 5.. – Fetchez la vache Dec 20 '16 at 10:53
  • @MarkWalters you are absolutely right, JS does not seem to support it. Good to know! – juanlu Dec 20 '16 at 14:13
  • Is there any way you could actually enter the link description there? – Heretic Monkey Dec 20 '16 at 14:36
  • @MikeMcCaughan I've just seen what you mean, that's an error on my part, the link is mentioned at the bottom. – Mark Walters Dec 20 '16 at 16:01
  • I was talking about the stuff you just edited out -- "enter link description here" at the beginning. Default link text bothers me for some reason; I also can't look at designs with "Lorem ipsum" too long ;). – Heretic Monkey Dec 20 '16 at 16:03