DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at

Regex - Character Ranges

Ranges help in matching characters by specifying start and end characters.

// Ranges are specified in [].
// Range specifies to match numbers & alphabets only from a to f
const colorRegex = /[0-9a-f]{6}/gi;

const colorCode1 = "#abc324";
console.log(colorCode1.match(colorRegex)); // ['abc324']

const colorCode2 = "#asc324";
console.log(colorCode2.match(colorRegex)); // null
Enter fullscreen mode Exit fullscreen mode

Thanks for reading 💙

Follow for daily posts.


Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

Discussion (0)