@gruhn/regex-utils
    Preparing search index...

    Function intersection

    • Takes a sequence of regular expressions and constructs their intersection. This is useful to combine several constraints into one. For example, to build a regular expression that can validate a new password:

      import { intersection } from '@gruhn/regex-utils'

      const passwordRegex = intersection(
      /.{12,}/, // 12 letters or more
      /[0-9]/, // at least one number
      /[A-Z]/, // at least one upper case letter
      /[a-z]/, // at least one lower case letter
      )

      function isValidPassword(str: string) {
      return passwordRegex.test(str)
      }

      In most cases it's simpler and more efficient to match each RegExp individually:

      function isValidPassword(str: string) {
      return /.{12,}/.test(str) && /[0-9]/.test(str) && /[A-Z]/.test(str) && /[a-z]/.test(str)
      }

      However, this is not always possible. For example, when a third-party interface expect a single RegExp as input like:

      • Express.js - for route parameter matching and path specifications
      • Yup/Joi/Zod - for string pattern validation
      • Webpack - in various configuration options like test, include, and exclude patterns
      • fast-check - for random string generation during fuzzing / property based testing

      Parameters

      • ...res: RegExp[]

      Returns RegExp