JavaScript - Check Hex in Opera?

This feature works in Firefox, Chrome, IE9. Doesn't work in Opera.

function isValidHex(hex) { alert(hex);
    var strPattern = /^#([0-9a-f]{1,2}){3}$/i; alert(strPattern.test(hex));
    return strPattern.test(hex);
}

The hexadecimal move is the same. Result strPatter.test returns false in Opera and true in Firefox.

Tested.

#000000
#ffffff

Any ideas?

+3
source share
2 answers

This regular expression is incorrect. #1234will be correct in this regular expression. User regex /^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/.

+2
source

If you change the multipliers, it also works in Opera:

function isValidHex(hex) {
  var objPattern = /^#([0-9a-f]{3}){1,2}$/i;
  return objPattern.test(hex);
}

, (, 3 3 + 3 ), (, 1 + 1 + 1, 2+ 1 + 1, 2 + 2 + 1 2 + 2 + 2 ).

( , varaible strPattern objPattern, , . , , .)

+2

Source: https://habr.com/ru/post/1771448/


All Articles