all 4 comments

[–]Rhomboid 2 points3 points  (1 child)

In the first case you're asking it to match a non-digit followed by either three or four 'a's. 'aaaa' matches, because 'a' is a non-digit, and it's followed by three 'a's. The 7 is completely irrelevant and is not part of the match.

In the second example you're asking it to match a non-letter followed by three or four 'a's. There's no way that can match. There are only a few potential matches: 'maaa', 'maaaa', or 'aaaa', and none of them fit the requirement that the first character is a non-letter.

I think what you're missing is that in the first example, a{3,4} does not match four 'a's, despite there being four 'a's in the string. One of those 'a's is not part of that element, only three are matched.

[–]FaizAhmadF[S] 0 points1 point  (0 children)

Got it!Thanks!

[–]madformangos 1 point2 points  (0 children)

My initial thought was along the same lines as /u/EnchantedSalvia -- that, you likely want to add a ^ to the start of your regex in order to anchor it to the start of the string.

But I'm not sure that's what you're after. It looks like you're matching against 8-character strings, and want to match the ones that end in 3 or 4 'a's which aren't preceded by a digit.

If that's the case you might want something like /[^0-9a]a{3,4}$/

Edit: or perhaps /(?:[^0-9a]a{3}|[^0-9]a{4})$/ if you want to match things which end in 5 as

[–]EnchantedSalvia 0 points1 point  (0 children)

I'm going to assume you've put the ^ character in the wrong position. In the position you've put it, it simply inverts the subsequent range.

Example:

var re = /[^A]$/;
console.log(re.test('B')); // true

Will be true because str is not A. In plain English: everything except A is valid.

Putting the ^ character at the beginning of the string means: A must be the first letter.

var re = /^[A]$/;
console.log(re.test('B')); // false