DEV Community

SCDan0624
SCDan0624

Posted on

3 2

Javascript regex part 5 quick advanced shortcuts

Intro

In previous blogs we looked at matching ranges of letters and numbers using []. Now we will take a look at some shorter syntax for looking at matching a range of letters and numbers.

Matching all letters and numbers

Previously when we wanted to search for all letter and numbers in the alphabet we had to use [a-z].

While there isn't a shortcut for letters only, there is a shortcut to search for letters and numbers using \w:

let short = /\w/
let myNumber = 23
let myPlayer = "Jordan"
short.test(myNumber) //true
short.test(myPlayer) // true
Enter fullscreen mode Exit fullscreen mode

You can also use /W if you want to search everything but numbers and letters:

let short = /\W/
let myNumber = "45*"
let myPlayer = "Jordan!"
myNumber.match(short) //*
myPlayer.match(short) //!
Enter fullscreen mode Exit fullscreen mode

Match all numbers

If you want to match all the numbers the shortcut for that is \d :

let movieName = "2002: Not A Space Odyssey";
let numRegex = /\d/g; // Change this line
let result = movieName.match(numRegex).length;

console.log(result) // 4
Enter fullscreen mode Exit fullscreen mode

If you want to match all non numbers use \D :

let movieName = "2002: Not A Space Odyssey";
let numRegex = /\D/g; // Change this line
let result = movieName.match(numRegex).length;

console.log(result) //21
Enter fullscreen mode Exit fullscreen mode

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay