-3

In javascript - I want to extract only the numbers that are exactly 5-digits long.

let example = "The quick brown 22 333 44 55555

In this case, I want it to match and give me and 55555

Edit: I figured it out. Since I want EXACTLY five digits: .match(/(?<!\d)\d{5}(?!\d)/g)

This ensures it's exactly five and no other numbers that exceed it

dekim2324
  • 15
  • 5

3 Answers3

1

This will do it.

(?<!\d)\d{5}(?!\d)

Demo

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
0

The regex you're looking for is:

/\d{5}/
  • \d for a digit
  • {5} for 5 times

An example:

const example = "hello c12345 df444 3444, 55555";

const matches = example.match(/\d{5}/g);

console.log(matches);
// => [ '12345', '55555' ]
Zorzi
  • 718
  • 4
  • 9
0
"[0-9]{5}"
[0-9] // match any numbers between 0 to 9
{5} // match exactly 5 times
thiru
  • 23
  • 2