About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Regular expression to match a hexadecimal color.
npm install @stdlib/regexp-color-hexadecimal
var reColorHexadecimal = require( '@stdlib/regexp-color-hexadecimal' );
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
Regular expression to match a full length hexadecimal color.
var bool = reColorHexadecimal.REGEXP.test( 'ffffff' );
// returns true
bool = reColorHexadecimal.REGEXP.test( '000' );
// returns false
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
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
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
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.
See LICENSE.
Copyright © 2016-2024. The Stdlib Authors.