├── .editorconfig ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── index.js ├── package-lock.json ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # .editorconfig 2 | # 3 | # Copyright (c) 2015 Charlike Mike Reagent, contributors. 4 | # Released under the MIT license. 5 | # 6 | 7 | root = true 8 | 9 | [*] 10 | charset = utf-8 11 | end_of_line = lf 12 | indent_size = 2 13 | indent_style = space 14 | 15 | [*.js] 16 | insert_final_newline = true 17 | trim_trailing_whitespace = true 18 | 19 | [*.php] 20 | indent_size = 4 21 | insert_final_newline = true 22 | trim_trailing_whitespace = true 23 | 24 | [*.md] 25 | insert_final_newline = false 26 | trim_trailing_whitespace = false 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # .gitignore 2 | # 3 | # Copyright (c) 2015 Charlike Mike Reagent, contributors. 4 | # Released under the MIT license. 5 | # 6 | 7 | # Always-ignore dirs # 8 | # #################### 9 | _gh_pages 10 | node_modules 11 | bower_components 12 | components 13 | vendor 14 | build 15 | dest 16 | dist 17 | src 18 | lib-cov 19 | coverage 20 | nbproject 21 | cache 22 | temp 23 | tmp 24 | hex-color-regex 25 | 26 | # Packages # 27 | # ########## 28 | *.7z 29 | *.dmg 30 | *.gz 31 | *.iso 32 | *.jar 33 | *.rar 34 | *.tar 35 | *.zip 36 | 37 | # OS, Logs and databases # 38 | # ######################### 39 | *.pid 40 | *.dat 41 | *.log 42 | *.sql 43 | *.sqlite 44 | *~ 45 | ~* 46 | 47 | # Another files # 48 | # ############### 49 | Icon? 50 | .DS_Store* 51 | Thumbs.db 52 | ehthumbs.db 53 | Desktop.ini 54 | npm-debug.log 55 | .directory 56 | ._* 57 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: "node_js" 2 | sudo: false 3 | 4 | node_js: 5 | - "0.8" 6 | - "0.10" 7 | - "0.12" 8 | - "1" 9 | - "2" 10 | 11 | notifications: 12 | email: 13 | on_success: never 14 | on_failure: never 15 | 16 | before_script: 17 | - npm install standard 18 | - standard 19 | 20 | script: 21 | - npm install istanbul-harmony 22 | - node --harmony node_modules/.bin/istanbul cover test.js 23 | 24 | after_success: 25 | - npm install coveralls 26 | - cat ./coverage/lcov.info | coveralls 27 | 28 | matrix: 29 | allow_failures: 30 | - node_js: "0.8" 31 | - node_js: "0.10" -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## 1.1.0 / 2017-06-03 4 | - Release v1.1.0 5 | - Add support for "alpha" channel in hex colors (merge [#11](https://github.com/regexhq/hex-color-regex/pull/11)): 6 | - `#RRGGBBAA` 7 | - `#RGBA` 8 | 9 | Reference 10 | - https://googlechrome.github.io/samples/css-alpha-channel/ 11 | - https://www.chromestatus.com/feature/5685348285808640 12 | 13 | ## 1.0.3 / 2015-07-14 14 | - Release v1.0.3 / npm@v1.0.3 15 | - update stuff, add related libs, @tunnckoCore 16 | - merge pull request #8, @kevva 17 | - Null guard, @bevacqua 18 | 19 | ## 1.0.2 / 2015-06-01 20 | - Release v1.0.2 / npm@v1.0.2 21 | - david to watch for devDeps 22 | - matching groups, close #6 23 | - update readme example 24 | - add more tests 25 | - switch coveralls with codeclimate coverage 26 | - add and update tests 27 | - update, close #5 28 | - add strict mode, merge #4 29 | 30 | ## 1.0.1 / 2015-03-26 31 | - Release v1.0.1 / npm@v1.0.1 32 | - bump deps 33 | - add `bluebird` as devDep 34 | 35 | ## 1.0.0 / 2015-02-03 36 | - Release v1.0.0 / npm@v1.0.0 37 | - add keywords 38 | - add usage examples 39 | - update tests 40 | - remove `after_script` from travis 41 | - replace `mocha` with `mukla` 42 | - fix regex 43 | 44 | ## 0.0.0 - 2015-02-02 45 | - first commits -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Contributions are always welcome! 4 | 5 | **Before spending lots of time on something, ask for feedback on your idea first!** 6 | 7 | Please search issues and pull requests before adding something new to avoid duplicating efforts and conversations. 8 | 9 | 10 | ## Installing 11 | 12 | Fork and clone the repo, then `npm install` to install all dependencies and `npm test` to ensure all is okey before you start anything. 13 | 14 | 15 | ## Testing 16 | 17 | Tests are run with `npm test`. Please ensure all tests are passing before submitting a pull request (unless you're creating a failing test to increase test coverage or show a problem). 18 | 19 | ## Code Style 20 | 21 | [![standard][standard-image]][standard-url] 22 | 23 | This repository uses [`standard`][standard-url] to maintain code style and consistency, and to avoid style arguments. You are encouraged to install it globally. `npm test` runs `standard` so you don't have to! 24 | 25 | ``` 26 | npm i standard -g 27 | ``` 28 | 29 | It is intentional to don't have `standard`, `istanbul` and `coveralls` in the devDependencies. Travis will handle all that stuffs. That approach will save bandwidth also installing and development time. 30 | 31 | [standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg 32 | [standard-url]: https://github.com/feross/standard -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License 2 | 3 | Copyright (c) 2015 [Charlike Make Reagent](http://j.mp/1stW47C) 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | > SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [hex-color-regex][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url] 2 | 3 | > The best regular expression (regex) for matching hex color values from string. 4 | 5 | [![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] 6 | 7 | 8 | ## Install 9 | ``` 10 | npm i hex-color-regex --save 11 | npm test 12 | ``` 13 | 14 | 15 | ## Usage 16 | > For more use-cases see the [tests](./test.js) 17 | 18 | - `[opts]` **{Object}** pass `strict: true` for strict mode 19 | - `return` **{RegExp}** 20 | 21 | **Example** 22 | 23 | ```js 24 | var hexColorRegex = require('hex-color-regex') 25 | 26 | hexColorRegex().test('#f3f}') //=> true 27 | hexColorRegex({strict: true}).test('#f3f}') //=> false 28 | 29 | hexColorRegex().test('foo #f3f bar') //=> true 30 | hexColorRegex({strict: true}).test('foo #f3f bar') //=> false 31 | 32 | hexColorRegex().test('#a54f2c}') //=> true 33 | hexColorRegex({strict: true}).test('#a54f2c}') //=> false 34 | 35 | hexColorRegex().test('foo #a54f2c bar') //=> true 36 | hexColorRegex({strict: true}).test('foo #a54f2c bar') //=> false 37 | 38 | hexColorRegex().test('#ffff') //=> false 39 | hexColorRegex().test('ffff') //=> false 40 | 41 | hexColorRegex().test('#fff') //=> true 42 | hexColorRegex().test('fff') //=> false 43 | 44 | hexColorRegex().test('#4g1') //=> false 45 | hexColorRegex().test('4g1') //=> false 46 | hexColorRegex().test('#zY1') //=> false 47 | hexColorRegex().test('zY1') //=> false 48 | hexColorRegex().test('#7f68ZY') //=> false 49 | hexColorRegex().test('7f68ZY') //=> false 50 | hexColorRegex().test('ffffff') //=> false 51 | 52 | hexColorRegex().test('#afebe3') //=> true 53 | hexColorRegex().test('#AFEBE3') //=> true 54 | hexColorRegex().test('#3cb371') //=> true 55 | hexColorRegex().test('#3CB371') //=> true 56 | hexColorRegex().test('#556b2f') //=> true 57 | hexColorRegex().test('#556B2F') //=> true 58 | hexColorRegex().test('#708090') //=> true 59 | hexColorRegex().test('#7b68ee') //=> true 60 | hexColorRegex().test('#7B68EE') //=> true 61 | hexColorRegex().test('#eeeeee') //=> true 62 | hexColorRegex().test('#ffffff') //=> true 63 | hexColorRegex().test('#111111') //=> true 64 | 65 | hexColorRegex().test('#afe') //=> true 66 | hexColorRegex().test('#AF3') //=> true 67 | hexColorRegex().test('#3cb') //=> true 68 | hexColorRegex().test('#3CB') //=> true 69 | hexColorRegex().test('#b2f') //=> true 70 | hexColorRegex().test('#5B2') //=> true 71 | hexColorRegex().test('#708') //=> true 72 | hexColorRegex().test('#68e') //=> true 73 | hexColorRegex().test('#7AF') //=> true 74 | hexColorRegex().test('#777') //=> true 75 | hexColorRegex().test('#FFF') //=> true 76 | hexColorRegex().test('#fff') //=> true 77 | ``` 78 | 79 | 80 | ## Matching groups 81 | 82 | - `match[0]` hex value with hash - `#f3f3f3` 83 | - `match[1]` hex value without the hash - `f3f3f3` 84 | 85 | **Example** 86 | 87 | ```js 88 | hexColorRegex().exec('foo #fff bar') 89 | //=> [ '#fff', 'fff', index: 4, input: 'foo #fff bar' ] 90 | 91 | hexColorRegex({strict: true}).exec('foo #fff bar') 92 | //=> null 93 | 94 | hexColorRegex().exec('foo #f3f3f3 bar') 95 | //=> [ '#f3f3f3', 'f3f3f3', index: 4, input: 'foo #f3f3f3 bar' ] 96 | 97 | hexColorRegex({strict: true}).exec('foo #f3f3f3 bar') 98 | //=> null 99 | ``` 100 | 101 | 102 | ## Related 103 | - [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) 104 | - [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) 105 | - [is-ansi](https://github.com/tunnckocore/is-ansi): Check that given string contain ANSI color codes, without CLI 106 | - [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) 107 | - [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) 108 | - [is-typeof-error](https://github.com/tunnckocore/is-typeof-error): Check that given value is any type of error and instanceof Error 109 | - [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) 110 | - [kind-error](https://github.com/tunnckocore/kind-error): Correct inheriting from `Error`. Supports constructing from an object of properties - focused on assertion. 111 | - [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) 112 | - [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) 113 | 114 | 115 | ## Contributing 116 | 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). 117 | But before doing anything, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) guidelines. 118 | 119 | 120 | ## [Charlike Make Reagent](http://j.mp/1stW47C) [![new message to charlike][new-message-img]][new-message-url] [![freenode #charlike][freenode-img]][freenode-url] 121 | 122 | [![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] 123 | 124 | 125 | [npmjs-url]: https://www.npmjs.com/package/hex-color-regex 126 | [npmjs-img]: https://img.shields.io/npm/v/hex-color-regex.svg?label=hex-color-regex 127 | 128 | [license-url]: https://github.com/regexhq/hex-color-regex/blob/master/LICENSE.md 129 | [license-img]: https://img.shields.io/badge/license-MIT-blue.svg 130 | 131 | 132 | [codeclimate-url]: https://codeclimate.com/github/regexps/hex-color-regex 133 | [codeclimate-img]: https://img.shields.io/codeclimate/github/regexps/hex-color-regex.svg 134 | 135 | [coverage-url]: https://codeclimate.com/github/regexps/hex-color-regex 136 | [coverage-img]: https://img.shields.io/codeclimate/coverage/github/regexps/hex-color-regex.svg 137 | 138 | [travis-url]: https://travis-ci.org/regexhq/hex-color-regex 139 | [travis-img]: https://img.shields.io/travis/regexhq/hex-color-regex.svg 140 | 141 | [coveralls-url]: https://coveralls.io/r/regexhq/hex-color-regex 142 | [coveralls-img]: https://img.shields.io/coveralls/regexhq/hex-color-regex.svg 143 | 144 | [david-url]: https://david-dm.org/regexhq/hex-color-regex 145 | [david-img]: https://img.shields.io/david/dev/regexhq/hex-color-regex.svg 146 | 147 | [standard-url]: https://github.com/feross/standard 148 | [standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg 149 | 150 | 151 | [author-www-url]: http://www.tunnckocore.tk 152 | [author-www-img]: https://img.shields.io/badge/www-tunnckocore.tk-fe7d37.svg 153 | 154 | [keybase-url]: https://keybase.io/tunnckocore 155 | [keybase-img]: https://img.shields.io/badge/keybase-tunnckocore-8a7967.svg 156 | 157 | [author-npm-url]: https://www.npmjs.com/~tunnckocore 158 | [author-npm-img]: https://img.shields.io/badge/npm-~tunnckocore-cb3837.svg 159 | 160 | [author-twitter-url]: https://twitter.com/tunnckoCore 161 | [author-twitter-img]: https://img.shields.io/badge/twitter-@tunnckoCore-55acee.svg 162 | 163 | [author-github-url]: https://github.com/tunnckoCore 164 | [author-github-img]: https://img.shields.io/badge/github-@tunnckoCore-4183c4.svg 165 | 166 | [freenode-url]: http://webchat.freenode.net/?channels=charlike 167 | [freenode-img]: https://img.shields.io/badge/freenode-%23charlike-5654a4.svg 168 | 169 | [new-message-url]: https://github.com/tunnckoCore/messages 170 | [new-message-img]: https://img.shields.io/badge/send%20me-message-green.svg 171 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * hex-color-regex 3 | * 4 | * Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) 5 | * Released under the MIT license. 6 | */ 7 | 8 | 'use strict' 9 | 10 | module.exports = function hexColorRegex(opts) { 11 | opts = opts && typeof opts === 'object' ? opts : {} 12 | 13 | return opts.strict 14 | ? /^#([a-f0-9]{3,4}|[a-f0-9]{4}(?:[a-f0-9]{2}){1,2})\b$/i 15 | : /#([a-f0-9]{3}|[a-f0-9]{4}(?:[a-f0-9]{2}){0,2})\b/gi 16 | } 17 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hex-color-regex", 3 | "version": "1.0.3", 4 | "lockfileVersion": 1, 5 | "dependencies": { 6 | "always-done": { 7 | "version": "1.1.0", 8 | "resolved": "https://registry.npmjs.org/always-done/-/always-done-1.1.0.tgz", 9 | "integrity": "sha1-hwV39QaHDZYq30tce5pcpn4lkfc=", 10 | "dev": true 11 | }, 12 | "arr-includes": { 13 | "version": "2.0.3", 14 | "resolved": "https://registry.npmjs.org/arr-includes/-/arr-includes-2.0.3.tgz", 15 | "integrity": "sha1-l20SxAWkUuLwQ+btYBwAnKsgeyA=", 16 | "dev": true 17 | }, 18 | "arrify": { 19 | "version": "1.0.1", 20 | "resolved": "https://registry.npmjs.org/arrify/-/arrify-1.0.1.tgz", 21 | "integrity": "sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=", 22 | "dev": true 23 | }, 24 | "asap": { 25 | "version": "2.0.5", 26 | "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.5.tgz", 27 | "integrity": "sha1-UidltQw1EEkOUtfc/ghe+bqWlY8=", 28 | "dev": true 29 | }, 30 | "buf-compare": { 31 | "version": "1.0.1", 32 | "resolved": "https://registry.npmjs.org/buf-compare/-/buf-compare-1.0.1.tgz", 33 | "integrity": "sha1-/vKNqLgROgoNtEMLC2Rntpcws0o=", 34 | "dev": true 35 | }, 36 | "clean-stacktrace": { 37 | "version": "1.1.0", 38 | "resolved": "https://registry.npmjs.org/clean-stacktrace/-/clean-stacktrace-1.1.0.tgz", 39 | "integrity": "sha1-i4zch/ZA2qupxZWrbtuJe2OwzjM=", 40 | "dev": true 41 | }, 42 | "clean-stacktrace-metadata": { 43 | "version": "1.0.6", 44 | "resolved": "https://registry.npmjs.org/clean-stacktrace-metadata/-/clean-stacktrace-metadata-1.0.6.tgz", 45 | "integrity": "sha1-4Px7Wb5CggujnEa0UoC7RFtBKpY=", 46 | "dev": true 47 | }, 48 | "clean-stacktrace-relative-paths": { 49 | "version": "1.0.4", 50 | "resolved": "https://registry.npmjs.org/clean-stacktrace-relative-paths/-/clean-stacktrace-relative-paths-1.0.4.tgz", 51 | "integrity": "sha1-sxjOBZq6sx885YqnAlddIFvzufQ=", 52 | "dev": true 53 | }, 54 | "common-callback-names": { 55 | "version": "2.0.1", 56 | "resolved": "https://registry.npmjs.org/common-callback-names/-/common-callback-names-2.0.1.tgz", 57 | "integrity": "sha1-b58H05PlnbK2jmA79q/9obs+B/c=", 58 | "dev": true 59 | }, 60 | "core-assert": { 61 | "version": "0.2.1", 62 | "resolved": "https://registry.npmjs.org/core-assert/-/core-assert-0.2.1.tgz", 63 | "integrity": "sha1-+F4s+b/tKPdzzIs/pcW2m9wC/j8=", 64 | "dev": true 65 | }, 66 | "dezalgo": { 67 | "version": "1.0.3", 68 | "resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.3.tgz", 69 | "integrity": "sha1-f3Qt4Gb8dIvI24IFad3c5Jvw1FY=", 70 | "dev": true 71 | }, 72 | "error-symbol": { 73 | "version": "0.1.0", 74 | "resolved": "https://registry.npmjs.org/error-symbol/-/error-symbol-0.1.0.tgz", 75 | "integrity": "sha1-Ck2uN9YA0VopukU9jvkg8YRDM/Y=", 76 | "dev": true 77 | }, 78 | "extend-shallow": { 79 | "version": "2.0.1", 80 | "resolved": "https://registry.npmjs.org/extend-shallow/-/extend-shallow-2.0.1.tgz", 81 | "integrity": "sha1-Ua99YUrZqfYQ6huvu5idaxxWiQ8=", 82 | "dev": true 83 | }, 84 | "find-callsite": { 85 | "version": "1.1.3", 86 | "resolved": "https://registry.npmjs.org/find-callsite/-/find-callsite-1.1.3.tgz", 87 | "integrity": "sha1-iU6HNrqJtlBLsD9kBj2Se19kCqA=", 88 | "dev": true 89 | }, 90 | "fn-name": { 91 | "version": "2.0.1", 92 | "resolved": "https://registry.npmjs.org/fn-name/-/fn-name-2.0.1.tgz", 93 | "integrity": "sha1-UhTXU3pNBqSjAcDMJi/rhBiAAuc=", 94 | "dev": true 95 | }, 96 | "function-arguments": { 97 | "version": "1.0.8", 98 | "resolved": "https://registry.npmjs.org/function-arguments/-/function-arguments-1.0.8.tgz", 99 | "integrity": "sha1-uaAdrKa4lO/4w9NoQDde2WNqbA8=", 100 | "dev": true 101 | }, 102 | "get-fn-name": { 103 | "version": "1.0.0", 104 | "resolved": "https://registry.npmjs.org/get-fn-name/-/get-fn-name-1.0.0.tgz", 105 | "integrity": "sha1-Cqj63PmVmOvLRMqw8afpVHLDFsk=", 106 | "dev": true 107 | }, 108 | "is-async-function": { 109 | "version": "1.2.3", 110 | "resolved": "https://registry.npmjs.org/is-async-function/-/is-async-function-1.2.3.tgz", 111 | "integrity": "sha1-z0IrYqUBnWsDJVaYlrfRFAPdPXY=", 112 | "dev": true 113 | }, 114 | "is-buffer": { 115 | "version": "1.1.5", 116 | "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.5.tgz", 117 | "integrity": "sha1-Hzsm72E7IUuIy8ojzGwB2Hlh7sw=", 118 | "dev": true 119 | }, 120 | "is-child-process": { 121 | "version": "1.0.2", 122 | "resolved": "https://registry.npmjs.org/is-child-process/-/is-child-process-1.0.2.tgz", 123 | "integrity": "sha1-wilhrNYp4SjLAI7WNVs7y/hdm9g=", 124 | "dev": true 125 | }, 126 | "is-error": { 127 | "version": "2.2.1", 128 | "resolved": "https://registry.npmjs.org/is-error/-/is-error-2.2.1.tgz", 129 | "integrity": "sha1-aEqW2EB2V3yY9M20DG0mpRI78Zw=", 130 | "dev": true 131 | }, 132 | "is-extendable": { 133 | "version": "0.1.1", 134 | "resolved": "https://registry.npmjs.org/is-extendable/-/is-extendable-0.1.1.tgz", 135 | "integrity": "sha1-YrEQ4omkcUGOPsNqYX1HLjAd/Ik=", 136 | "dev": true 137 | }, 138 | "is-node-emitter": { 139 | "version": "1.0.6", 140 | "resolved": "https://registry.npmjs.org/is-node-emitter/-/is-node-emitter-1.0.6.tgz", 141 | "integrity": "sha1-gHqLAZTOzPmbb31elaOfHJV+qjY=", 142 | "dev": true 143 | }, 144 | "is-node-stream": { 145 | "version": "1.0.0", 146 | "resolved": "https://registry.npmjs.org/is-node-stream/-/is-node-stream-1.0.0.tgz", 147 | "integrity": "sha1-+OGNp78aZqZS2IYMg0qyxsem6JY=", 148 | "dev": true 149 | }, 150 | "is-promise": { 151 | "version": "2.1.0", 152 | "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.1.0.tgz", 153 | "integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o=", 154 | "dev": true 155 | }, 156 | "is-real-object": { 157 | "version": "1.0.2", 158 | "resolved": "https://registry.npmjs.org/is-real-object/-/is-real-object-1.0.2.tgz", 159 | "integrity": "sha1-3RcOrYiCnDjF1LQwWW2R4rl3OIM=", 160 | "dev": true 161 | }, 162 | "is-request-stream": { 163 | "version": "1.0.1", 164 | "resolved": "https://registry.npmjs.org/is-request-stream/-/is-request-stream-1.0.1.tgz", 165 | "integrity": "sha1-XL/c7ynojEelaA789pk0YnhS38E=", 166 | "dev": true 167 | }, 168 | "is-typeof-error": { 169 | "version": "1.1.0", 170 | "resolved": "https://registry.npmjs.org/is-typeof-error/-/is-typeof-error-1.1.0.tgz", 171 | "integrity": "sha1-+CTiQTQsBniwnWl+gEGutPT6KBw=", 172 | "dev": true 173 | }, 174 | "isarray": { 175 | "version": "1.0.0", 176 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", 177 | "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", 178 | "dev": true 179 | }, 180 | "kind-of": { 181 | "version": "3.2.2", 182 | "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", 183 | "integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=", 184 | "dev": true 185 | }, 186 | "lazy-cache": { 187 | "version": "2.0.2", 188 | "resolved": "https://registry.npmjs.org/lazy-cache/-/lazy-cache-2.0.2.tgz", 189 | "integrity": "sha1-uRkKT5EzVGlIQIWfio9whNiCImQ=", 190 | "dev": true 191 | }, 192 | "mukla": { 193 | "version": "0.4.9", 194 | "resolved": "https://registry.npmjs.org/mukla/-/mukla-0.4.9.tgz", 195 | "integrity": "sha1-GVzzlUFUWX41WZ/btKE8KkHXM/Q=", 196 | "dev": true 197 | }, 198 | "on-stream-end": { 199 | "version": "1.0.0", 200 | "resolved": "https://registry.npmjs.org/on-stream-end/-/on-stream-end-1.0.0.tgz", 201 | "integrity": "sha1-iTkmHfb9dR8S78qEiDRqQSn1+nM=", 202 | "dev": true 203 | }, 204 | "once": { 205 | "version": "1.4.0", 206 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", 207 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", 208 | "dev": true 209 | }, 210 | "onetime": { 211 | "version": "1.1.0", 212 | "resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", 213 | "integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k=", 214 | "dev": true 215 | }, 216 | "set-getter": { 217 | "version": "0.1.0", 218 | "resolved": "https://registry.npmjs.org/set-getter/-/set-getter-0.1.0.tgz", 219 | "integrity": "sha1-12nBgsnVpR9AkUXy+6guXoboA3Y=", 220 | "dev": true 221 | }, 222 | "stack-utils-node-internals": { 223 | "version": "1.0.1", 224 | "resolved": "https://registry.npmjs.org/stack-utils-node-internals/-/stack-utils-node-internals-1.0.1.tgz", 225 | "integrity": "sha1-q0qKRptsvscrC/tYnfXiix0SKB8=", 226 | "dev": true 227 | }, 228 | "stacktrace-metadata": { 229 | "version": "2.0.4", 230 | "resolved": "https://registry.npmjs.org/stacktrace-metadata/-/stacktrace-metadata-2.0.4.tgz", 231 | "integrity": "sha1-/RJu7HEumYFl38nthIqSul1LEds=", 232 | "dev": true 233 | }, 234 | "stream-exhaust": { 235 | "version": "1.0.1", 236 | "resolved": "https://registry.npmjs.org/stream-exhaust/-/stream-exhaust-1.0.1.tgz", 237 | "integrity": "sha1-wMRFXlTOWhecqHNuczNLTn/WdVM=", 238 | "dev": true 239 | }, 240 | "success-symbol": { 241 | "version": "0.1.0", 242 | "resolved": "https://registry.npmjs.org/success-symbol/-/success-symbol-0.1.0.tgz", 243 | "integrity": "sha1-JAIuSG878c3KCUKDt2nEctO3KJc=", 244 | "dev": true 245 | }, 246 | "to-object-path": { 247 | "version": "0.3.0", 248 | "resolved": "https://registry.npmjs.org/to-object-path/-/to-object-path-0.3.0.tgz", 249 | "integrity": "sha1-KXWIt7Dn4KwI4E5nL4XB9JmeF68=", 250 | "dev": true 251 | }, 252 | "try-catch-callback": { 253 | "version": "2.0.2", 254 | "resolved": "https://registry.npmjs.org/try-catch-callback/-/try-catch-callback-2.0.2.tgz", 255 | "integrity": "sha1-PKce+kJC2KRe4cAskb0XoVDN1TQ=", 256 | "dev": true 257 | }, 258 | "try-catch-core": { 259 | "version": "2.0.3", 260 | "resolved": "https://registry.npmjs.org/try-catch-core/-/try-catch-core-2.0.3.tgz", 261 | "integrity": "sha1-eDXMsdRytxZ9aIyMxPfU9pXu+iI=", 262 | "dev": true 263 | }, 264 | "wrappy": { 265 | "version": "1.0.2", 266 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", 267 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", 268 | "dev": true 269 | } 270 | } 271 | } 272 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hex-color-regex", 3 | "version": "1.1.0", 4 | "description": "The best regular expression (regex) for matching hex color values from string.", 5 | "repository": "regexps/hex-color-regex", 6 | "author": "Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)", 7 | "main": "index.js", 8 | "license": "MIT", 9 | "scripts": { 10 | "test": "standard && node test.js" 11 | }, 12 | "dependencies": {}, 13 | "devDependencies": { 14 | "mukla": "^0.4.9" 15 | }, 16 | "keywords": [ 17 | "color", 18 | "colors", 19 | "css", 20 | "expr", 21 | "expression", 22 | "expressions", 23 | "hex", 24 | "match", 25 | "matching", 26 | "regex", 27 | "regexp", 28 | "regexps", 29 | "regular", 30 | "web" 31 | ] 32 | } 33 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * hex-color-regex 3 | * 4 | * Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) 5 | * Released under the MIT license. 6 | */ 7 | 8 | /* jshint asi:true */ 9 | 10 | 'use strict' 11 | 12 | var test = require('mukla') 13 | var hexColorRegex = require('./index') 14 | 15 | var sixDigits = { 16 | pass: [ 17 | '#afebe3', 18 | '#AFEBE3', 19 | '#3cb371', 20 | '#3CB371', 21 | '#556b2f', 22 | '#556B2F', 23 | '#708090', 24 | '#7b68ee', 25 | '#7B68EE', 26 | '#eeeeee', 27 | '#ffffff', 28 | '#123fff}', 29 | '#111111' 30 | ], 31 | fail: [ 32 | 'afebe3', 33 | 'AFEBE3', 34 | '3cb371', 35 | 'ABC371', 36 | '556b2f', 37 | '5A6B2F', 38 | '708090', 39 | '7b68ee', 40 | '7B68EE', 41 | 'eeeeee', 42 | 'ffffff', 43 | '111111', 44 | 'afebef', 45 | '3c537f', 46 | '556B2f', 47 | '708135', 48 | 'EE3EF1', 49 | '7f68ZY', 50 | '#7f68ZY', 51 | '#7z68ZY', 52 | '#GR68', 53 | '#Z68', 54 | '#666EFR' 55 | ] 56 | } 57 | 58 | var threeDigits = { 59 | pass: [ 60 | '#afe', 61 | '#AF3', 62 | '#3cb', 63 | '#3CB', 64 | '#b2f', 65 | '#5B2', 66 | '#708', 67 | '#68e', 68 | '#7AF', 69 | '#777', 70 | '#FFF', 71 | '#fff', 72 | '#f3f}', 73 | '#111' 74 | ], 75 | fail: [ 76 | 'fff', 77 | '4zy', 78 | '4g1', 79 | '111', 80 | 'Ge3', 81 | 'zY1', 82 | '#ggg', 83 | '#4zy', 84 | '#4g1', 85 | '#Ge3', 86 | '#zY1' 87 | ] 88 | } 89 | 90 | var fourDigits = { 91 | pass: ['#afe0', '#AF31', '#3cba', '#3CBA', '#b2ff', '#5B2F'], 92 | fail: ['afe0', 'AF31', '#3cbg', '#3CBy', '#b2fz'] 93 | } 94 | 95 | var eightDigits = { 96 | pass: ['#afebe300', '#AFEBE3AA', '#3cb371ff', '#3CB371CC', '#556b2f55'], 97 | fail: ['afebe300', 'AFEBE3AA', '#3cb371fg', '#3CB371xy', '#556b2fz9'] 98 | } 99 | 100 | test('hex-color-regex:', function() { 101 | test('in no strict mode', function() { 102 | test('six digit hex', function() { 103 | test('should be `true`', function() { 104 | sixDigits.pass.forEach(function(hex) { 105 | test('when `' + hex + '` value', function() { 106 | test.equal(hexColorRegex().test(hex), true) 107 | }) 108 | }) 109 | test('when `foo #ae3f4c bar` value', function() { 110 | test.equal(hexColorRegex().test('foo #ae3f4c bar'), true) 111 | }) 112 | }) 113 | test('should be `false`', function() { 114 | sixDigits.fail.forEach(function(hex) { 115 | test('when `' + hex + '` value', function() { 116 | test.equal(hexColorRegex().test(hex), false) 117 | }) 118 | }) 119 | }) 120 | }) 121 | test('three digit hex', function() { 122 | test('should be `true`', function() { 123 | threeDigits.pass.forEach(function(hex) { 124 | test('when `' + hex + '` value', function() { 125 | test.equal(hexColorRegex().test(hex), true) 126 | }) 127 | }) 128 | test('when `foo #e4f bar` value', function() { 129 | test.equal(hexColorRegex().test('foo #e4f bar'), true) 130 | }) 131 | }) 132 | test('should be `false`', function() { 133 | threeDigits.fail.forEach(function(hex) { 134 | test('when `' + hex + '` value', function() { 135 | test.equal(hexColorRegex().test(hex), false) 136 | }) 137 | }) 138 | }) 139 | }) 140 | test('eight digit alpha channel hex', function() { 141 | test('should be `true`', function() { 142 | eightDigits.pass.forEach(function(hex) { 143 | test('when `' + hex + '` value', function() { 144 | test.equal(hexColorRegex().test(hex), true) 145 | }) 146 | }) 147 | test('when `foo #ae3f4c bar` value', function() { 148 | test.equal(hexColorRegex().test('foo #ae3f4c00 bar'), true) 149 | }) 150 | }) 151 | test('should be `false`', function() { 152 | eightDigits.fail.forEach(function(hex) { 153 | test('when `' + hex + '` value', function() { 154 | test.equal(hexColorRegex().test(hex), false) 155 | }) 156 | }) 157 | }) 158 | }) 159 | test('four digit alpha channel hex', function() { 160 | test('should be `true`', function() { 161 | fourDigits.pass.forEach(function(hex) { 162 | test('when `' + hex + '` value', function() { 163 | test.equal(hexColorRegex().test(hex), true) 164 | }) 165 | }) 166 | test('when `foo #ae3f4c bar` value', function() { 167 | test.equal(hexColorRegex().test('foo #ae3f bar'), true) 168 | }) 169 | }) 170 | test('should be `false`', function() { 171 | fourDigits.fail.forEach(function(hex) { 172 | test('when `' + hex + '` value', function() { 173 | test.equal(hexColorRegex().test(hex), false) 174 | }) 175 | }) 176 | }) 177 | }) 178 | test('using regex().exec(hex)', function() { 179 | sixDigits.pass.forEach(function(hex) { 180 | var hexed = hex.replace('}', '') 181 | test('should match `' + hexed + '` when `' + hex + '` hex', function() { 182 | var actual = hexColorRegex().exec(hex)[0] 183 | var expected = hexed 184 | 185 | test.equal(actual, expected) 186 | }) 187 | }) 188 | test('should match `#ae3f4c` when `foo #ae3f4c bar` string', function() { 189 | var actual = hexColorRegex().exec('foo #ae3f4c bar')[0] 190 | var expected = '#ae3f4c' 191 | 192 | test.equal(actual, expected) 193 | }) 194 | threeDigits.pass.forEach(function(hex) { 195 | var hexed = hex.replace('}', '') 196 | test('should match `' + hexed + '` when `' + hex + '` hex', function() { 197 | var actual = hexColorRegex().exec(hex)[0] 198 | var expected = hexed 199 | 200 | test.equal(actual, expected) 201 | }) 202 | }) 203 | test('should match `#e7f` when `foo #e7f bar` string', function() { 204 | var actual = hexColorRegex().exec('foo #e7f bar')[0] 205 | var expected = '#e7f' 206 | 207 | test.equal(actual, expected) 208 | }) 209 | eightDigits.pass.forEach(function(hex) { 210 | var hexed = hex.replace('}', '') 211 | test('should match `' + hexed + '` when `' + hex + '` hex', function() { 212 | var actual = hexColorRegex().exec(hex)[0] 213 | var expected = hexed 214 | 215 | test.equal(actual, expected) 216 | }) 217 | }) 218 | test('should match `#ae3f4c00` when `foo #ae3f4c00 bar` string', function() { 219 | var actual = hexColorRegex().exec('foo #ae3f4c00 bar')[0] 220 | var expected = '#ae3f4c00' 221 | 222 | test.equal(actual, expected) 223 | }) 224 | fourDigits.pass.forEach(function(hex) { 225 | var hexed = hex.replace('}', '') 226 | test('should match `' + hexed + '` when `' + hex + '` hex', function() { 227 | var actual = hexColorRegex().exec(hex)[0] 228 | var expected = hexed 229 | 230 | test.equal(actual, expected) 231 | }) 232 | }) 233 | test('should match `#e7f0` when `foo #e7f0 bar` string', function() { 234 | var actual = hexColorRegex().exec('foo #e7f0 bar')[0] 235 | var expected = '#e7f0' 236 | 237 | test.equal(actual, expected) 238 | }) 239 | }) 240 | }) 241 | test('in strict mode', function() { 242 | test('six digit hex `#123fff}` should return false', function() { 243 | test.equal(hexColorRegex({ strict: true }).test('#123fff}'), false) 244 | }) 245 | test('string contain six digit hex `foo #ae3f4c bar` return false', function() { 246 | test.equal(hexColorRegex({ strict: true }).test('foo #ae3f4c bar'), false) 247 | }) 248 | test('three digit hex `#f3f}` should return false', function() { 249 | test.equal(hexColorRegex({ strict: true }).test('#f3f}'), false) 250 | }) 251 | test('string contain three digit hex `foo #e7f bar` return false', function() { 252 | test.equal(hexColorRegex({ strict: true }).test('foo #e7f bar'), false) 253 | }) 254 | test('eight digit alpha channel hex `#123fff00}` should return false', function() { 255 | test.equal(hexColorRegex({ strict: true }).test('#123fff00}'), false) 256 | }) 257 | test('string contain eight digit alpha channel hex `foo #ae3f4cff bar` return false', function() { 258 | test.equal( 259 | hexColorRegex({ strict: true }).test('foo #ae3f4cff bar'), 260 | false 261 | ) 262 | }) 263 | test('four digit alpha channel hex `#f3f0}` should return false', function() { 264 | test.equal(hexColorRegex({ strict: true }).test('#f3f0}'), false) 265 | }) 266 | test('string contain four digit alpha channel hex `foo #e7ff bar` return false', function() { 267 | test.equal(hexColorRegex({ strict: true }).test('foo #e7ff bar'), false) 268 | }) 269 | test('should not match when `foo #ae3f4c bar` string', function() { 270 | var actual = hexColorRegex({ strict: true }).exec('foo #ae3f4c bar') 271 | var expected = null 272 | 273 | test.equal(actual, expected) 274 | }) 275 | test('should not match when `foo #e7f bar` string', function() { 276 | var actual = hexColorRegex({ strict: true }).exec('foo #e7f bar') 277 | var expected = null 278 | 279 | test.equal(actual, expected) 280 | }) 281 | test('should not match when `foo #ae3f4cff bar` string', function() { 282 | var actual = hexColorRegex({ strict: true }).exec('foo #ae3f4cff bar') 283 | var expected = null 284 | 285 | test.equal(actual, expected) 286 | }) 287 | test('should not match when `foo #e7ff bar` string', function() { 288 | var actual = hexColorRegex({ strict: true }).exec('foo #e7ff bar') 289 | var expected = null 290 | 291 | test.equal(actual, expected) 292 | }) 293 | }) 294 | }) 295 | --------------------------------------------------------------------------------