Home > Web Front-end > JS Tutorial > body text

How to Restrict Input Length in Regular Expressions Using Lookaheads?

Linda Hamilton
Release: 2024-11-22 16:00:27
Original
159 people have browsed it

How to Restrict Input Length in Regular Expressions Using Lookaheads?

Restricting Input Length in Regular Expressions

Regular expressions provide a powerful way to match patterns in text, and it's often necessary to restrict the length of characters matched. While this may seem straightforward, it can be challenging to apply quantifiers correctly. In this article, we'll explore why limiting quantifiers at the end of a pattern doesn't work and provide an alternative approach using lookaheads.

Consider the following regular expression:

/(a-z|A-Z|0-9)*[^$%^&*;:,<>?()""']*$/
Copy after login

This expression matches a sequence of letters, digits, and other characters, but it doesn't restrict the total length of the input string. To limit the characters to 15, we might try:

/(a-z|A-Z|0-9)*[^$%^&*;:,<>?()""']*${1,15}/
Copy after login

However, this will result in an error. The reason is that quantifiers apply to the subpattern immediately to their left, not the entire pattern. In this case, the quantifier {1,15} applies to the second character class, limiting its length to 1 to 15 characters, but not the overall string length.

Instead, the correct way to restrict the input length is to use a lookahead anchored at the beginning of the string:

^(?=.{1,15}$)[a-zA-Z0-9]*[^$%^&*;:,<>?()""']*$
Copy after login
  • The ^ anchor matches the start of the string.
  • The positive lookahead (?=.{1,15}$) checks if there are 1 to 15 characters before the end of the string ($).
  • The remaining character class matches letters, digits, and other allowed characters.

This lookahead ensures that the entire input string matches the desired length restriction.

Note: Lookaheads are zero-width assertions that do not consume any characters. They only return true or false based on the subsequent pattern.

In cases where the input can contain newlines, you can use the portable [sS] character class to match any character, including newlines:

^(?=[\s\S]{1,15}$)[a-zA-Z0-9]*[^$%^&*;:,<>?()""']*$
Copy after login

By using lookaheads, we can effectively restrict the length of the input string while maintaining the desired pattern matching behavior.

The above is the detailed content of How to Restrict Input Length in Regular Expressions Using Lookaheads?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template