In Javascript Regex, how to check that a string is a valid hex color?

For a string like #fff443 or #999999

How to check that a string has:

  • 7 characters, the first of which is a hash
  • there are no characters in the string except the hash at the beginning
+6
source share
1 answer

It seems that you are matching with css color:

 function isValidColor(str) { return str.match(/^#[a-f0-9]{6}$/i) !== null; } 

Develop:

^ start of the match
# hash [a-f0-9] any letter from af and 0-9
{6} previous group is displayed exactly 6 times
$ end of match
i ignore case

+18
source

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


All Articles