Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

'use strict';

module.exports = function hexColorRegex() {
var regex = /#([a-f0-9]{6}|[a-f0-9]{3})\b/gi
module.exports = function hexColorRegex(config) {
config = config || {};
var regex = config.strict ? /^#([a-f0-9]{6}|[a-f0-9]{3})\b$/gi : /#([a-f0-9]{6}|[a-f0-9]{3})\b/gi
return regex;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hex-color-regex",
"version": "1.0.1",
"version": "1.1.0",
"description": "Regular expression (regex) for matching hex color values from string.",
"scripts": {
"lint": "jshint index.js && jscs index.js --reporter inline",
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ npm i --save hex-color-regex
npm test
```

## Parameters

### strict

_default: false_

Tests also to see if the hex is the _only_ token passed in (adds ^ to the beginning and $ to the end.)

## Usage
> For more use-cases see the [tests](./test.js)
Expand All @@ -28,6 +35,7 @@ hexColorRegex().test('708135') //=> false
hexColorRegex().test('ffffff') //=> false
hexColorRegex().test('afebef') //=> false
hexColorRegex().test('#113141}') //=> false, for now
hexColorRegex({strict:true}).test('http://www.example.com/index.html#f06d06}') //=> false

hexColorRegex().test('#afebe3') //=> true
hexColorRegex().test('#AFEBE3') //=> true
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ var threeDigits = {
]
};

var urlTest = 'http://www.example.com/index.html#f06d06';

sixDigits.pass.forEach(function(hex) {
mukla('should be `true` when `'+ hex +'` value').strictEqual(regex().test(hex), true);
});
Expand All @@ -97,3 +99,7 @@ threeDigits.pass.forEach(function(hex) {
threeDigits.fail.forEach(function(hex) {
mukla('should be `false` when `'+ hex +'` hex value').strictEqual(regex().test(hex), false);
});
mukla('should be `false` when `' + urlTest + '` is passed in with strict mode').strictEqual(regex({strict:true}).test(urlTest), false);
mukla('should be `true` when `' + urlTest + '` is passed in without strict mode').strictEqual(regex().test(urlTest), true);