Categories
JavaScript Answers

How to match 6 digit hexadecimal numbers with JavaScript regular expressions?

Spread the love

Sometimes, we want to match 6 digit hexadecimal numbers with JavaScript regular expressions.

In this article, we’ll look at how to match 6 digit hexadecimal numbers with JavaScript regular expressions.

How to match 6 digit hexadecimal numbers with JavaScript regular expressions?

To match 6 digit hexadecimal numbers with JavaScript regular expressions, we can use the /[0-9A-Fa-f]{6}/g regex.

For instance, we write:

console.log(/[0-9A-Fa-f]{6}/g.test('ffffff'))
console.log(/[0-9A-Fa-f]{6}/g.test('abc'))

to check if 'ffffff' and 'abc' are valid 6 digit hex numbers with the regex test method.

Since 'ffffff' is a valid 6 digit hex number, the first log logs true.

And since 'abc' isn’t a valid 6 digit hex number, the first log logs false.

Conclusion

To match 6 digit hexadecimal numbers with JavaScript regular expressions, we can use the /[0-9A-Fa-f]{6}/g regex.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

2 replies on “How to match 6 digit hexadecimal numbers with JavaScript regular expressions?”

Leave a Reply

Your email address will not be published. Required fields are marked *