最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - In Javascript Regex, how do I validate that a string is a valid hexadecimal colour? - Stack Overflow

programmeradmin1浏览0评论

Given a string like #fff443 or #999999

How do I verify that the string has:

  • 7 characters, with the first one being a hash
  • no symbols in the string besides the hash in the beginning

Given a string like #fff443 or #999999

How do I verify that the string has:

  • 7 characters, with the first one being a hash
  • no symbols in the string besides the hash in the beginning
Share Improve this question edited Jan 2, 2014 at 13:03 Jelle De Loecker 21.9k29 gold badges104 silver badges146 bronze badges asked Jan 15, 2012 at 9:44 TIMEXTIMEX 272k367 gold badges800 silver badges1.1k bronze badges 1
  • 1 there are many ways to check with different structures : see my advanced answer : stackoverflow.com/questions/8027423/… – Royi Namir Commented Jan 15, 2012 at 10:05
Add a comment  | 

1 Answer 1

Reset to default 23

It seems that you are matching against a css color:

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

To elaborate:

^ match beginning
# a hash
[a-f0-9] any letter from a-f and 0-9
{6} the previous group appears exactly 6 times
$ match end
i ignore case

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论