The following provides a complete list and description of the special characters that can be used in regular expressions.
Table 1.3: Special characters in regular expressions:
character\
Meaning: For characters, it usually means literal meaning, indicating that the next character is a special character, and is not explained.
For example: /b/ matches the character 'b'. By adding a backslash\, that is, /\b/, the character becomes a special character, indicating the dividing line matching a word.
or:
For several characters, the description is usually special, indicating that the following characters are not special, but should be interpreted literally.
For example: * is a special character that matches any character (including 0 characters); for example: /a*/ means matching 0 or more a. To match the literal *, add a backslash before a; for example: /a\*/match 'a*'.
Character^
Meaning: The character that indicates that the matching must be at the front.
For example: /^A/ does not match 'A' in "an A," but matches 'A' in "An A."
Character $
Meaning: Similar to ^, matching the last character.
For example: /t$/ does not match 't' in "eater", but match 't' in "eater".
character*
Meaning: Match * 0 or n times before the characters.
For example:/bo*/ matches 'boooo' in "A ghost booooed" or 'b' in "A bird warbled", but does not match any characters in "A goat grunted".
Character +
Meaning: Match the characters before the + sign 1 or n times. Equivalent to {1,}.
For example: /a+/ matches all 'a' in "candy" and "caaaaaaandy."
character?
Meaning: Match? 0 or 1 previous character.
For example: /e?le?/matches 'el' in "angel" and 'le' in "angle."
character.
Meaning: (decimal point) matches all individual characters except line breaks.
For example: /.n/ matches 'an' and 'on' in "nay, an apple is on the tree", but does not match 'nay'.
Characters (x)
Meaning: Match 'x' and record the matching value.
For example: /(foo)/Match and record 'foo' in "foo bar." The matching substring can be returned by the element [1], ..., [n] in the result array, or by the attributes $1, ..., $9 of the RegExp object.
Character x|y
Meaning: Match 'x' or 'y'.
For example: /green|red/ matches 'green' in "green apple" and 'red' in "red apple."
Character {n}
Meaning: n here is a positive integer. Match the previous n characters.
For example: /a{2}/ does not match 'a' in "candy," but matches all 'a' in "caandy," and the first two 'a' in "caaandy."
Character {n,}
Meaning: n here is a positive integer. Match at least n preceding characters.
For example: /a{2,} does not match 'a' in "candy", but matches all 'a' in "caandy" and all 'a' in "caaaaaaandy."
Character {n,m}
Meaning: n and m here are both positive integers. Match at least n at most m previous characters.
For example: /a{1,3}/ does not match any character in "cndy", but matches the 'a' in "candy,", the first two 'a' in "caandy," and the first three 'a' in "caaaaaandy", note: Even if there are many 'a' in "caaaaaandy", it only matches the previous three
'a' is "aaa".
Characters [xyz]
Meaning: a list of characters, matching any character listed. You can point out a character range by hyphen.
For example: [abcd] is the same as [a-c]. They match 'b' in "brisket" and 'c' in "ache".
Characters [^xyz]
Meaning: One-character complement, that is, it matches everything except the listed characters. You can use hyphen - point out a character range.
For example: [^abc] and [^a-c] are equivalent, they first match 'r' in "brisket" and 'h' in "chop."
Characters [\b]
Meaning: Match a space (not to be confused with \b)
Character\b
Meaning: Match the dividing line of a word, such as a space (not to be confused with [\b])
For example: /\bn\w/ matches 'no' in "noonday", /\wy\b/ matches 'ly' in "possibly yesterday."
Character\B
Meaning: Match a word's non-dividing line
For example: /\w\Bn/ matches 'on' in "noonday", /y\B\w/ matches 'ye' in "possibly yesterday."
Characters\cX
Meaning: The X here is a control character. Match a string of control characters.
For example: /\cM/ matches control-M in a string.
Character\d
Meaning: Matching a number is equivalent to [0-9].
For example: /\d/ or /[0-9]/ matches '2' in "B2 is the suite number."
Characters\D
Meaning: Match any non-digit, equivalent to [^0-9].
For example: /\D/ or /[^0-9]/ matches 'B' in "B2 is the suite number."
Character\f
Meaning: Match a form character
Character\n
Meaning: Match a newline
Characters\r
Meaning: Match a carriage return
Characters\s
Meaning: Match a single white space character, including space, tab, form feed, line feed, equivalent to [ \f\n\r\t\v].
For example: /\s\w*/ matches 'bar' in "foo bar."
Characters\S
Meaning: Matching a single character other than the white space character is equivalent to [^ \f\n\r\t\v].
For example: /\S/\w* matches 'foo' in "foo bar."
Characters\t
Meaning: Match a tab character
Character\v
Meaning: Match a header tab
Characters\w
Meaning: Match all numbers and letters and underscores, equivalent to [A-Za-z0-9_].
For example: /\w/ matches 'a' in "apple," , '5' in "$5.28," and '3' in "3D."
Character\W
Meaning: Matching other characters except numbers, letters and underscores is equivalent to [^A-Za-z0-9_].
For example: /\W/ or /[^$A-Za-z0-9_]/match '%' in "50%.".
Character\n
Meaning: n here is a positive integer. Matches the value of n of the last substring of a regular expression (counting the left bracket).
For example: /apple(,)\sorange\1/ matches 'apple, orange' in "apple, orange, cherry, peach." There is a more complete example below.
Note: If the number in the left parentheses is smaller than the number specified by \n, then the octal escape of the next row is taken as the description.
Meaning: Here \ooctal is an octal escape value, while \xhex is a hex escape value that allows the ASCII code to be embedded in a regular expression.