JavaScript Regular Expression Patterns

The pattern can be a combination of characters, operators, and quantifiers.

  • Characters: The pattern can include any character.
  • Operators: The pattern can include operators that match specific patterns. For example, the . operator matches any character.
  • Quantifiers: The pattern can include quantifiers that match a specific number of characters. For example, the * quantifier matches zero or more characters.

Matching a Specific String

To match a specific string, simply specify it in the regular expression.

let regex = /apple/;

Matching Multiple Options

You can use the pipe character | to match multiple options.

let regex = /apple|banana/;

This regex will match either “apple” or “banana.”

Matching Any Character

The dot . in a regular expression matches any character except a newline.

let regex = /a./;

This regex will match “ab,” “ac,” “ad,” and so on.

Here are some examples of JavaScript regular expressions:

/[a-z]/ // Matches any lowercase letter.
/[0-9]/ // Matches any digit.
/./ // Matches any character.
/\d+/ // Matches one or more digits.
/\w+/ // Matches one or more word characters.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT