Споделяне чрез


Lists of Matching Characters (JavaScript)

You can create a list of matching characters by enclosing one or more individual characters in brackets [ ]. When characters are enclosed in brackets, the list is called a bracket expression.

Characters enclosed in a bracket expression match only a single character for the position in the regular expression where the bracket expression appears. The following JavaScript regular expression matches "Chapter 1", "Chapter 2", "Chapter 3", "Chapter 4", and "Chapter 5".

/Chapter [12345]/

The bracket expression is used to specify only the set of characters that matches the single character position immediately following the word Chapter and a space. That is, the ninth character position.

Characters in Bracket Expressions

Within brackets, as anywhere else, ordinary characters represent themselves. That is, they match an occurrence of themselves in the searched text. Most special characters lose their meaning when they occur inside a bracket expression. Following are some exceptions.

  • The close bracket character (]) ends a list if it is not the first item. To match the close bracket character in a list, include it first, immediately following the opening bracket ([).

  • The backslash character (\) continues to be the escape character. To match the backslash character, use two backslashes (\\).

Using Hyphens for Character Ranges

If you want to express the matching characters by using a range instead of the characters themselves, you can use the hyphen (-) character to separate the starting and ending characters of the range. The following regular expression is equivalent to /Chapter [12345]/.

/Chapter [1-5]/

Both the starting and ending values are included in the range. It is important to note that the starting value must precede the ending value in Unicode sort order. The character values of the individual characters determine their relative order in a range.

A typical use of a bracket expression is to specify matches of any uppercase or lowercase alphabetical characters or any digits. The following expression specifies such a match.

/[A-Za-z0-9]/  

Including a Hyphen Character in an Expression

To include the hyphen character in a bracket expression, you can use one of the following methods:

  • Precede it with a backslash escape character, as shown in the following expression.

    [\-]
    
  • Put the hyphen character at the start or the end of the bracketed list. The following expressions match all lowercase letters and the hyphen.

    [-a-z]  
    [a-z-] 
    
  • Create a range in which the starting character value is lower than the hyphen character and the ending character value is equal to or greater than the hyphen. Both of the following regular expressions satisfy these requirements.

    [!--]
    [!-~] 
    

Using a Caret for Negation

You can also find all characters that are not in a list or range by including the caret (^) character at the start of the list. If the caret character appears in any other position in the list, it matches itself; that is, it has no special meaning. The following regular expression matches chapter headings that do not contain characters 1 through 5.

/Chapter [^12345]/

In the previous examples, the expression matches any character in the ninth position except 1, 2, 3, 4, or 5. Therefore, for example, "Chapter 7" is a match and so is "Chapter 9".

You can represent the same expressions by using the hyphen character (-).The following expression specifies such a match.

/Chapter [^1-5]/

Matching Any Character

The period (.) matches any single printing or non-printing character in a string, except a newline character (\n). The regular expression /a.c/ in JavaScript matches "aac", "abc", "acc", "adc", "a1c", "a2c", "a-c", and "a#c".

To match a period (.) that is contained in the searched string, you can precede the period in the regular expression with a backslash (\). The regular expression /filename\.ext/ matches "filename.ext".

See Also

Concepts

Creating a Regular Expression (JavaScript)