Home > Software design >  How to get the last four digits of phone number using regex in Javascript?
How to get the last four digits of phone number using regex in Javascript?

Time:06-11

Suppose I have a string: phone = '12345678' I want to extract only the last 4 digits of the string using regex. How to do it?

CodePudding user response:

Use /\d{4}$/ pattern.

JavaScript Example

let phone = '12345678';
console.log(phone.match(/\d{4}$/)[0]);  // "5678"
  • Related