Skip to main content

Quantifiers

Regular Expressions: Quantifiers


What is a quantifier used for in a regular expression?

View Answer:
Interview Response: We can use a quantifier to find the exact count in a sequence of digits. For example, if we are looking for a sequence of numbers like 123-456. We can used \d{3} which will return 123,456.

Code Example:

// Sequence of digits
alert("I'm 12345 years old".match(/\d{5}/)); // "12345"

// Range of digits
alert("I'm not 12, but 1234 years old".match(/\d{3,5}/)); // "1234"

// Omitting the upper range with \d{3,}
alert("I'm not 12, but 345678 years old".match(/\d{3,}/)); // "345678"

// Implementation on a range and omitting the upper range
let str = '+7(903)-123-45-67';

let numbers = str.match(/\d{1,}/g);

alert(numbers); // 7,903,123,45,67

Are there any shorthand alias quantifiers in regular expressions?

View Answer:
Interview Response: There are shorthands for most used quantifiers, like \d+ which looks for numbers and is a shorthand way for \d{1,},. Quantifiers are often employed, and they are the fundamental "building block" of sophisticated regular expressions. Using shorthand aliases can help reduce the code necessary to implement an expression.

Code Example:

let str = '+7(903)-123-45-67';
alert(str.match(/\d+/g)); // 7,903,123,45,67

let str = 'Should I write color or colour?';
alert(str.match(/colou?r/g)); // color, colour

alert('100 10 1'.match(/\d0*/g)); // 100, 10, 1

alert('100 10 1'.match(/\d0+/g)); // 100, 10
// 1 not matched, as 0+ requires at least one zero