You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
7.6 KiB

4 years ago
  1. # [hex-color-regex][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url]
  2. > The best regular expression (regex) for matching hex color values from string.
  3. [![code climate][codeclimate-img]][codeclimate-url] [![standard code style][standard-img]][standard-url] [![travis build status][travis-img]][travis-url] [![coverage status][coverage-img]][coverage-url] [![dependency status][david-img]][david-url]
  4. ## Install
  5. ```
  6. npm i hex-color-regex --save
  7. npm test
  8. ```
  9. ## Usage
  10. > For more use-cases see the [tests](./test.js)
  11. - `[opts]` **{Object}** pass `strict: true` for strict mode
  12. - `return` **{RegExp}**
  13. **Example**
  14. ```js
  15. var hexColorRegex = require('hex-color-regex')
  16. hexColorRegex().test('#f3f}') //=> true
  17. hexColorRegex({strict: true}).test('#f3f}') //=> false
  18. hexColorRegex().test('foo #f3f bar') //=> true
  19. hexColorRegex({strict: true}).test('foo #f3f bar') //=> false
  20. hexColorRegex().test('#a54f2c}') //=> true
  21. hexColorRegex({strict: true}).test('#a54f2c}') //=> false
  22. hexColorRegex().test('foo #a54f2c bar') //=> true
  23. hexColorRegex({strict: true}).test('foo #a54f2c bar') //=> false
  24. hexColorRegex().test('#ffff') //=> false
  25. hexColorRegex().test('ffff') //=> false
  26. hexColorRegex().test('#fff') //=> true
  27. hexColorRegex().test('fff') //=> false
  28. hexColorRegex().test('#4g1') //=> false
  29. hexColorRegex().test('4g1') //=> false
  30. hexColorRegex().test('#zY1') //=> false
  31. hexColorRegex().test('zY1') //=> false
  32. hexColorRegex().test('#7f68ZY') //=> false
  33. hexColorRegex().test('7f68ZY') //=> false
  34. hexColorRegex().test('ffffff') //=> false
  35. hexColorRegex().test('#afebe3') //=> true
  36. hexColorRegex().test('#AFEBE3') //=> true
  37. hexColorRegex().test('#3cb371') //=> true
  38. hexColorRegex().test('#3CB371') //=> true
  39. hexColorRegex().test('#556b2f') //=> true
  40. hexColorRegex().test('#556B2F') //=> true
  41. hexColorRegex().test('#708090') //=> true
  42. hexColorRegex().test('#7b68ee') //=> true
  43. hexColorRegex().test('#7B68EE') //=> true
  44. hexColorRegex().test('#eeeeee') //=> true
  45. hexColorRegex().test('#ffffff') //=> true
  46. hexColorRegex().test('#111111') //=> true
  47. hexColorRegex().test('#afe') //=> true
  48. hexColorRegex().test('#AF3') //=> true
  49. hexColorRegex().test('#3cb') //=> true
  50. hexColorRegex().test('#3CB') //=> true
  51. hexColorRegex().test('#b2f') //=> true
  52. hexColorRegex().test('#5B2') //=> true
  53. hexColorRegex().test('#708') //=> true
  54. hexColorRegex().test('#68e') //=> true
  55. hexColorRegex().test('#7AF') //=> true
  56. hexColorRegex().test('#777') //=> true
  57. hexColorRegex().test('#FFF') //=> true
  58. hexColorRegex().test('#fff') //=> true
  59. ```
  60. ## Matching groups
  61. - `match[0]` hex value with hash - `#f3f3f3`
  62. - `match[1]` hex value without the hash - `f3f3f3`
  63. **Example**
  64. ```js
  65. hexColorRegex().exec('foo #fff bar')
  66. //=> [ '#fff', 'fff', index: 4, input: 'foo #fff bar' ]
  67. hexColorRegex({strict: true}).exec('foo #fff bar')
  68. //=> null
  69. hexColorRegex().exec('foo #f3f3f3 bar')
  70. //=> [ '#f3f3f3', 'f3f3f3', index: 4, input: 'foo #f3f3f3 bar' ]
  71. hexColorRegex({strict: true}).exec('foo #f3f3f3 bar')
  72. //=> null
  73. ```
  74. ## Related
  75. - [benz](https://github.com/tunnckocore/benz): Compose your control flow with absolute elegance. Support async/await, callbacks, thunks, generators, promises, observables, child… [more](https://github.com/tunnckocore/benz)
  76. - [is-hexcolor](https://github.com/tunnckocore/is-hexcolor): Check that given value is valid hex color, using `hex-color-regex` - the best regex for… [more](https://github.com/tunnckocore/is-hexcolor)
  77. - [is-ansi](https://github.com/tunnckocore/is-ansi): Check that given string contain ANSI color codes, without CLI
  78. - [is-missing](https://github.com/tunnckocore/is-missing): Check that given `name` or `user/repo` exists in npm registry or in github as user… [more](https://github.com/tunnckocore/is-missing)
  79. - [is-kindof](https://github.com/tunnckocore/is-kindof): Check type of given javascript value. Support promises, generators, streams, and native types. Thin wrapper… [more](https://github.com/tunnckocore/is-kindof)
  80. - [is-typeof-error](https://github.com/tunnckocore/is-typeof-error): Check that given value is any type of error and instanceof Error
  81. - [is-async-function](https://github.com/tunnckocore/is-async-function): Check that given function is async (callback) function or not. Trying to guess that based… [more](https://github.com/tunnckocore/is-async-function)
  82. - [kind-error](https://github.com/tunnckocore/kind-error): Correct inheriting from `Error`. Supports constructing from an object of properties - focused on assertion.
  83. - [kind-of-extra](https://github.com/tunnckocore/kind-of-extra): Extends `kind-of` type check utility with support for promises, generators, streams and errors. Like `kindof(Promise.resolve(1))… [more](https://github.com/tunnckocore/kind-of-extra)
  84. - [vez](https://github.com/tunnckocore/vez): Middleware composition at new level. Ultimate alternative to `ware`, `plugins`, `koa-compose` and `composition` packages. Allows… [more](https://github.com/tunnckocore/vez)
  85. ## Contributing
  86. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/regexhq/hex-color-regex/issues/new).
  87. But before doing anything, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) guidelines.
  88. ## [Charlike Make Reagent](http://j.mp/1stW47C) [![new message to charlike][new-message-img]][new-message-url] [![freenode #charlike][freenode-img]][freenode-url]
  89. [![tunnckocore.tk][author-www-img]][author-www-url] [![keybase tunnckocore][keybase-img]][keybase-url] [![tunnckoCore npm][author-npm-img]][author-npm-url] [![tunnckoCore twitter][author-twitter-img]][author-twitter-url] [![tunnckoCore github][author-github-img]][author-github-url]
  90. [npmjs-url]: https://www.npmjs.com/package/hex-color-regex
  91. [npmjs-img]: https://img.shields.io/npm/v/hex-color-regex.svg?label=hex-color-regex
  92. [license-url]: https://github.com/regexhq/hex-color-regex/blob/master/LICENSE.md
  93. [license-img]: https://img.shields.io/badge/license-MIT-blue.svg
  94. [codeclimate-url]: https://codeclimate.com/github/regexps/hex-color-regex
  95. [codeclimate-img]: https://img.shields.io/codeclimate/github/regexps/hex-color-regex.svg
  96. [coverage-url]: https://codeclimate.com/github/regexps/hex-color-regex
  97. [coverage-img]: https://img.shields.io/codeclimate/coverage/github/regexps/hex-color-regex.svg
  98. [travis-url]: https://travis-ci.org/regexhq/hex-color-regex
  99. [travis-img]: https://img.shields.io/travis/regexhq/hex-color-regex.svg
  100. [coveralls-url]: https://coveralls.io/r/regexhq/hex-color-regex
  101. [coveralls-img]: https://img.shields.io/coveralls/regexhq/hex-color-regex.svg
  102. [david-url]: https://david-dm.org/regexhq/hex-color-regex
  103. [david-img]: https://img.shields.io/david/dev/regexhq/hex-color-regex.svg
  104. [standard-url]: https://github.com/feross/standard
  105. [standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
  106. [author-www-url]: http://www.tunnckocore.tk
  107. [author-www-img]: https://img.shields.io/badge/www-tunnckocore.tk-fe7d37.svg
  108. [keybase-url]: https://keybase.io/tunnckocore
  109. [keybase-img]: https://img.shields.io/badge/keybase-tunnckocore-8a7967.svg
  110. [author-npm-url]: https://www.npmjs.com/~tunnckocore
  111. [author-npm-img]: https://img.shields.io/badge/npm-~tunnckocore-cb3837.svg
  112. [author-twitter-url]: https://twitter.com/tunnckoCore
  113. [author-twitter-img]: https://img.shields.io/badge/twitter-@tunnckoCore-55acee.svg
  114. [author-github-url]: https://github.com/tunnckoCore
  115. [author-github-img]: https://img.shields.io/badge/github-@tunnckoCore-4183c4.svg
  116. [freenode-url]: http://webchat.freenode.net/?channels=charlike
  117. [freenode-img]: https://img.shields.io/badge/freenode-%23charlike-5654a4.svg
  118. [new-message-url]: https://github.com/tunnckoCore/messages
  119. [new-message-img]: https://img.shields.io/badge/send%20me-message-green.svg