Description

Regular Expression: using a single string to describe and match a series of strings that conform to a certain syntactic rule search pattern.
JavaScript regular expression syntax: /regular expression body/modifier (optional)
Modifier Description
i Perform a case-insensitive match
g Perform a global match (find all matches rather than stopping after the first match)
m Perform a multi-line match
Start And End
^ Indicates that the string must start with the character after the ^, when used in a bracket expression, it indicates that the character is not accepted in the bracket expression.
$ Indicates that the string must end with the character before the $.
Wildcards
Use wildcards and special escape characters to match more characters
  • . matches any character except for newline characters
  • \w matches a word character (alphanumeric character or underscore)
  • \W matches any character that is not a word character (not alphanumeric or underscore)
  • \b matches the position at the start or end of a word
  • \B matches any position that is not at the start or end of a word
  • \d matches a digit
  • \D matches any character that is not a digit
  • \s matches any whitespace character
  • \S matches any character that is not a whitespace character
  • \n matches a newline character
Specific quantity
Match a specific number of matching characters or groups with quantifiers
  • * Matches the preceding subexpression zero or more times
  • + Matches one or more times
  • ? Matches zero or one time
  • {n} Repeats n times
  • {n,} Repeats n times or more
  • {n,m} Repeats from n to m times
Parentheses matching groups
Use () to capture groups, () will return the full match plus the group, unless you use the g flag, use the pipe operator | inside () to specify what the group should match, equivalent to or.
Character groups[]
Character groups help match specific characters or specific character sequences. They can represent a large batch of characters like character shortcuts, such as \d matching the same characters as [0-9]. Negating a character group will match characters that do not match the character group. For example, if you don't want to match digits, you can write: [^0-9]
Backslash
To match special characters, use the backslash
The special characters in JavaScript regular expressions are: ^ $ \。 * +? () [] {} |
So, to match an asterisk, you can use \*, not just *
Common regular expressions
  • Match HTML attributes: \s*\S*="[^"]+"\s*
  • Match HTML links: <a [^>]*href\s*=\s*['""]([^'""]+)['""][^>]*>

0 Comments

0 / 300