Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Regular Expression – Position Matching

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

  1. Precise Targeting: Allows precisely specifying where a match occurs
  2. Efficiency Improvement: Avoids unnecessary full-text searches
  3. 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)


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

  1. Confusion^Usage in character classes:

  2. [^abc]represents "a character that is not a, b, or c"
  3. ^abcrepresents "a string that starts with abc"
  4. - `[^abc]` represents "a character that is not a, b, or c"; `^abc` represents "a string that starts with abc"
    1. Ignoring the Effect of Multiline Mode:

    2. By default^and$matches the start and end of the entire string
    3. in multiline mode they match the start and end of each line
    4. - By default `^` and `$` match the start and end of the entire string; in multiline mode they match the start and end of each line
      1. Boundary matching consumes no characters:

      // This pattern matches nothing because \b consumes no characters: const pattern = /\b\b/;


      Practice challenges

      1. Write a regular expression that matches all strings starting with "Chapter" followed by 1-2 digits
      2. Create a pattern that matches all standalone "the" words in a string (not "the" inside "there")
      3. 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: