Quantifiers in JavaScript Regular Expressions

Quantifiers in JavaScript Regular Expressions

Quantifiers in JavaScript Regular Expressions

Quantifiers in JavaScript regular expressions define the number of occurrences of the preceding character or group that should be matched. They are used to specify whether the preceding character is required to appear a certain number of times (or within a specific range).

Here’s a breakdown of the common quantifiers in regular expressions:

1. + (One or more)

The + quantifier matches one or more occurrences of the preceding element. It means the element must appear at least once, but can repeat any number of times.

Example: Matching One or More Digits

let pattern = /\d+/; let str = "12345"; console.log(pattern.test(str)); // true (matches "12345")
  • \d+ matches one or more digits. It successfully matches "12345" in the string "12345".
  • If you apply it to a string with no digits, it will not match.
let str = "hello"; console.log(pattern.test(str)); // false

2. * (Zero or more)

The * quantifier matches zero or more occurrences of the preceding element. It means the element is optional (can appear 0 or more times).

Example: Matching Zero or More Letters

let pattern = /a*/; let str = "aaaa"; console.log(pattern.test(str)); // true (matches "aaaa")
  • a* matches zero or more occurrences of the letter "a". It will match any number of "a"s, even zero (e.g., "", "a", "aa", etc.).

Example: Zero Occurrences

let pattern = /\d*/; let str = "hello123"; console.log(pattern.test(str)); // true (matches "123")
  • Here, \d* matches zero or more digits, so it matches the "123" part of the string "hello123". It also works if there are no digits present (the match would be an empty string).

3. ? (Zero or one)

The ? quantifier matches zero or one occurrence of the preceding element. It makes the preceding element optional, meaning it can either appear once or not at all.

Example: Matching Zero or One Character

let pattern = /b?/; let str = "b"; console.log(pattern.test(str)); // true (matches "b")
  • b? matches zero or one occurrence of the letter "b". It matches "b" in the string "b".

Example: Matching Optional Letters

let pattern = /colou?r/; let str = "color"; console.log(pattern.test(str)); // true (matches "color")
  • colou?r matches both "color" and "colour", as the "u" is optional in the pattern.

4. {n} (Exactly n times)

The {n} quantifier matches the exact number of occurrences of the preceding element. It allows you to specify how many times a character or group should appear.

Example: Matching Exactly Three Digits

let pattern = /\d{3}/; let str = "123"; console.log(pattern.test(str)); // true (matches "123")
  • \d{3} matches exactly three digits in a row. It matches "123" in the string "123", but it won't match if there are more or fewer digits.

Example: Matching Exactly Two Letters

let pattern = /[a-z]{2}/; let str = "ab"; console.log(pattern.test(str)); // true (matches "ab")
  • [a-z]{2} matches exactly two lowercase letters. It will match "ab" in "ab" but not "abc" or "a".

5. {n,} (At Least n Times)

The {n,} quantifier matches at least n occurrences of the preceding element. It ensures that the element appears n or more times.

Example: Matching At Least Three Digits

let pattern = /\d{3,}/; let str = "12345"; console.log(pattern.test(str)); // true (matches "12345")
  • \d{3,} matches at least three digits, and since "12345" has more than three digits, it matches the entire string.

Example: Matching At Least One Character

let pattern = /[a-z]{1,}/; let str = "hello"; console.log(pattern.test(str)); // true (matches "hello")
  • [a-z]{1,} matches at least one lowercase letter, so it matches "hello".

6. {n,m} (Between n and m Times)

The {n,m} quantifier matches between n and m occurrences of the preceding element. This gives you a way to specify a range for the number of occurrences.

Example: Matching Between Two and Four Digits

let pattern = /\d{2,4}/; let str = "123"; console.log(pattern.test(str)); // true (matches "123")
  • \d{2,4} matches any sequence of digits that is at least 2 digits long, but no more than 4 digits. It will match "123" in "123", but not "1" or "12345".

Example: Matching Between Three and Five Letters

let pattern = /[a-z]{3,5}/; let str = "hello"; console.log(pattern.test(str)); // true (matches "hello")
  • [a-z]{3,5} matches any sequence of 3 to 5 lowercase letters. It matches "hello" because it is 5 letters long.

7. Combining Quantifiers

You can combine quantifiers with other regular expression features to create more complex patterns.

Example: Matching One or More Words

let pattern = /\w+/; let str = "Hello world"; console.log(pattern.test(str)); // true (matches "Hello")
  • \w+ matches one or more word characters (letters, digits, or underscores). In the string "Hello world", it matches "Hello", and if you continue testing the rest of the string, it will also match "world".

8. Conclusion

Quantifiers are an essential part of JavaScript regular expressions that allow you to control how many times a character or pattern is matched:

  • +: One or more times.
  • *: Zero or more times.
  • ?: Zero or one time (optional).
  • {n}: Exactly n times.
  • {n,}: At least n times.
  • {n,m}: Between n and m times.

These quantifiers can be used to match patterns of varying lengths and help create flexible and powerful regular expressions.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close