contents   index   previous   next



Regular expression anchor characters

Anchor characters indicate that the following or preceding characters must be next to a special position in a string. The characters next to anchor characters are included in a match, not the anchor characters themselves. For example, in the string "The big cat and the small cat", the regular expression /cat$/ will match the "cat" at the end of the string, and the match will include only the three characters "cat". The "$" is an anchor character indicating the end of a string (or line if a multiline search is being done).

 

The following table lists the anchor characters, metacharacters, and their meanings.

 

Character

 

Anchor meaning

 

^

 

The beginning of a string (or line if doing a multiline search). (See \A below.)

 

Example: /^The/

 

$

 

The end of a string (or line if doing a multiline search). (See \Z below.)

 

Example: /cat$/

 

\A

 

Matches the beginning of a string only. (See $ above.)

 

\b

 

A word boundary. Match any character that is not considered to be a valid character for a word in programming. The character class "\W", not a word character, is similar. There are two differences. One, "\b" also matches a backspace. Two, "\W" is included in a match, since it is regular expression character, but "\b" is not included in a match.

 

Example: /\bthe\b/

 

\B

 

Not a word boundary. The character class "\w" is similar. The most notable difference is that "\w" is included in a match, and "\B" is not.

 

Example: /l\B/

 

\Z

 

Matches the end of a string only. (See ^ above.)

 


Regular expression reference characters