Regular Expression - Position Matching
Position matching (also called anchoring or boundary matching) refers to matching a specific position in a string rather than an actual character. Unlike ordinary character matching, position matching consumes no characters; it only specifies where a match must occur.
Why Position Matching Is Needed
- Precise Targeting: Allows precisely specifying where a match occurs
- Efficiency Improvement: Avoids unnecessary full-text searches
- Pattern Validation: Checks whether a string conforms to specific format requirements
Commonly Used Position Matching Metacharacters
1. Line Start and Line End Matching
^ - Matches the start of a line
Example
// Match lines starting with "Hello"
const
pattern
=
/^Hello/
;
console.
log
(
pattern.
test
(
"Hello World"
)
)
;
// true
console.
log
(
pattern.
test
(
"Say Hello"
)
)
;
// false
$ - Matches the end of a line
Example
// Match lines ending with "World"
const
pattern
=
/World$/
;
console.
log
(
pattern.
test
(
"Hello World"
)
)
;
// true
console.
log
(
pattern.
test
(
"World Peace"
)
)
;
// false
2. Word Boundary Matching
\b - Matches a word boundary
A word boundary refers to the position between\w([a-zA-Z0-9_]) and\Wa non-word character, or the start/end position of a string.
Example
// Match the standalone word "cat"
const
pattern
=
/\bcat\b/
;
console.
log
(
pattern.
test
(
"cat"
)
)
;
// true
console.
log
(
pattern.
test
(
"concatenate"
)
)
;
// false
console.
log
(
pattern.
test
(
"a cat"
)
)
;
// true
\B - Matches a non-word boundary
Example
// Match "cat" not at a word boundary
const
pattern
=
/\Bcat\B/
;
console.
log
(
pattern.
test
(
"concatenate"
)
)
;
// true
console.
log
(
pattern.
test
(
"cat"
)
)
;
// false
3. Other Position Matching
\A and \Z(supported in some languages)
\A: Matches the start of a string (unlike^, which is not affected by multiline mode)\Z: Matches the end of a string or the position before a trailing newline
Practical Applications of Position Matching
1. Validate Input Format
Example
// Validate phone number (starts with 1, 11 digits total)
const
phonePattern
=
/^1\d{10}$/
;
console.
log
(
phonePattern.
test
(
"13800138000"
)
)
;
// true
console.
log
(
phonePattern.
test
(
"a13800138000"
)
)
;
// false
2. Extract Words at Specific Positions
Example
// Extract the first word of each line
const
text
=
"Apple Banana\nCherry Date"
;
const
firstWords
=
text.
match
(
/^\w+/gm
)
;
console.
log
(
firstWords
)
;
// ["Apple", "Cherry"]
3. Replace Text at Specific Positions
Example
// Add "> " at the start of each paragraph
const
text
=
"First line\nSecond line"
;
const
result
=
text.
replace
(
/^/gm
,
"> "
)
;
console.
log
(
result
)
;
// > First line
// > Second line
Advanced Position Matching Techniques
1. Position Matching in Multiline Mode
Use m The flag (multiline mode) changes the behavior of ^ and $ :
Example
const
text
=
"Line 1\nLine 2\nLine 3"
;
// Normal mode
console.
log
(
text.
match
(
/^Line \d/g
)
)
;
// ["Line 1"]
// Multiline mode
console.
log
(
text.
match
(
/^Line \d/gm
)
)
;
// ["Line 1", "Line 2", "Line 3"]
2. Lookaround Assertions
Although not strictly position matching, lookaround assertions can help implement more complex positional conditions:
Positive Lookahead (?=)
Example
// Match numbers followed by "px"
const
pattern
=
/\d+(?=px)/
;
console.
log
(
pattern.
exec
(
"12px"
)
)
;
// ["12"]
Negative Lookahead (?!)
Example
// Match numbers not followed by "px"
const
pattern
=
/\d+(?!px)/
;
console.
log
(
pattern.
exec
(
"12em"
)
)
;
// ["12"]
Common errors and caveats
-
Confusion
^Usage in character classes:
[^abc]represents "a character that is not a, b, or c"^abcrepresents "a string that starts with abc"-
Ignoring the Effect of Multiline Mode:
^and$matches the start and end of the entire string-
Boundary matching consumes no characters:
// This pattern matches nothing because \b consumes no characters: const pattern = /\b\b/;
Practice challenges
- Write a regular expression that matches all strings starting with "Chapter" followed by 1-2 digits
- Create a pattern that matches all standalone "the" words in a string (not "the" inside "there")
- Write a regular expression to validate whether a string is a valid URL (starting with http:// or https://)
Summary of key points
| Metacharacter | Description | Example |
|---|---|---|
^ |
Matches the start of a line/string | ^Start |
$ |
Matches the end of a line/string | end$ |
\b |
Matches a word boundary | \bword\b |
\B |
Matches a non-word boundary | \Bword\B |
\A |
Matches the start of a string (some languages) | \AStart |
\Z |
Matches the end of a string (some languages) | end\Z |
Mastering position matching can significantly improve the precision and efficiency of regular expressions. Remember:
- Position matching consumes no characters; it only specifies where a match occurs
- Using boundary matching appropriately can avoid unnecessary full-text searches
- Multiline mode changes the behavior of
^and$.