Navigate back to the homepage

How to check if a string is hexadecimal in javascript?

Infinitbility
JavaScript
May 27th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show how do you check if a string is hexadecimal in javascript, here I will use the regex expression to check string is a hex code or not.

Here, we will write reg expression for validating string hex code, and javascript provides test() method validate string using regex, we are also going to do same.

Let’s start today’s tutorial How to check if a string is hexadecimal in javascript?

Here, we will do

  1. Write regex code which we will use
  2. Create valid and invalid hex code variables
  3. Use regex and test method to validate string
  4. Console which strings valid hex code which is not
1// Write regx code which we will use
2let regExp = /^#([0-9a-f]{3}){1,2}$/i;
3
4// Create valid and invalid hex code variables
5let validHex = "#AABBCC";
6let invalidHex = "#GHIJKL";
7
8// Use regex and test method to validate string
9// Console which string valid hex code which is not
10if(regExp.test(validHex)){
11 console.log("validHex have valid hex code.")
12}
13
14if(regExp.test(invalidHex)){
15 console.log("invalidHex have valid hex code.")
16}

When you run the above program, it will console which string variable contains valid hex code, let’s check the output.

javascript, check if a string is hexadecimal example
javascript, check if a string is hexadecimal example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if a string is a valid email in javascript?

Example of check if a string is a valid email string in javascript with step by step guide.

May 26th, 2022 · 1 min read

How to check if a string is empty or undefined in javascript?

Example of check if a string is empty or undefined in javascript with step by step guide.

May 26th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]