README
Hexadecimal Color
Regular expression to match a hexadecimal color.
Installation
npm install @stdlib/regexp-color-hexadecimal
Usage
var reColorHexadecimal = require( '@stdlib/regexp-color-hexadecimal' );
reColorHexadecimal( [mode] )
Returns a regular expression to match a full hexadecimal color.
var RE = reColorHexadecimal();
// returns <RegExp>
var bool = RE.test( 'ffffff' );
// returns true
bool = RE.test( '000' );
// returns false
To return a regular expression that matches a shorthand hexadecimal color, set the mode
argument to shorthand
.
var RE = reColorHexadecimal( 'shorthand' );
// returns <RegExp>
var bool = RE.test( '000' );
// returns true
To return a regular expression that matches either a shorthand or a full length hexadecimal color, set the mode
argument to either
.
var RE = reColorHexadecimal( 'either' );
// returns <RegExp>
var bool = RE.test( '000' );
// returns true
reColorHexadecimal.REGEXP
Regular expression to match a full length hexadecimal color.
var bool = reColorHexadecimal.REGEXP.test( 'ffffff' );
// returns true
bool = reColorHexadecimal.REGEXP.test( '000' );
// returns false
reColorHexadecimal.REGEXP_SHORTHAND
Regular expression to match a shorthand hexadecimal color.
var bool = reColorHexadecimal.REGEXP_SHORTHAND.test( 'ffffff' );
// returns false
bool = reColorHexadecimal.REGEXP_SHORTHAND.test( '000' );
// returns true
reColorHexadecimal.REGEXP_EITHER
Regular expression to match either a shorthand or a full length hexadecimal color.
var bool = reColorHexadecimal.REGEXP_EITHER.test( 'ffffff' );
// returns true
bool = reColorHexadecimal.REGEXP_EITHER.test( '000' );
// returns true
Examples
var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
var reColorHexadecimal = require( '@stdlib/regexp-color-hexadecimal' );
function isHexColor( value, mode ) {
if ( !isString( value ) ) {
return false;
}
if ( mode === 'shorthand' ) {
return reColorHexadecimal.REGEXP_SHORTHAND.test( value );
}
if ( mode === 'either' ) {
return reColorHexadecimal.REGEXP_EITHER.test( value );
}
return reColorHexadecimal.REGEXP.test( value );
}
var bool = isHexColor( 'ffffff', 'full' );
// returns true
bool = isHexColor( '474747', 'either' );
// returns true
bool = isHexColor( '0A5BBE', 'shorthand' );
// returns false
bool = isHexColor( '000', 'full' );
// returns false
bool = isHexColor( '000', 'either' );
// returns true
bool = isHexColor( 'F7b', 'shorthand' );
// returns true
bool = isHexColor( 'abcd', 'either' );
// returns false
bool = isHexColor( '', 'either' );
// returns false
bool = isHexColor( null, 'either' );
// returns false
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2022. The Stdlib Authors.