Anchors: String Start (^) and End ($) in JavaScript

Anchors: String Start (^) and End ($) in JavaScript

Anchors: String Start (^) and End ($) in JavaScript

In JavaScript regular expressions, anchors are special characters used to assert positions in a string. They do not match actual characters but instead match specific locations within the string. The most common anchors are:

  • ^: Matches the start of the string.
  • $: Matches the end of the string.

These anchors are useful when you want to ensure that a pattern occurs at the beginning or the end of a string.

1. Start Anchor (^)

The ^ anchor matches the beginning of a string. It is useful when you want to check if a string starts with a specific pattern or substring.

Example: Matching the Start of a String

let pattern = /^Hello/; let str1 = "Hello, World!"; let str2 = "Say Hello, World!"; console.log(pattern.test(str1)); // true (starts with "Hello") console.log(pattern.test(str2)); // false (does not start with "Hello")
  • ^Hello: Matches any string that starts with the word "Hello".

In the above example, str1 starts with "Hello", so the result is true. However, str2 does not start with "Hello", so the result is false.

2. End Anchor ($)

The $ anchor matches the end of a string. It is useful when you want to check if a string ends with a specific pattern or substring.

Example: Matching the End of a String

let pattern = /world!$/; let str1 = "Hello, world!"; let str2 = "world! Hello"; console.log(pattern.test(str1)); // true (ends with "world!") console.log(pattern.test(str2)); // false (does not end with "world!")
  • world!$: Matches any string that ends with the word "world!".

In this case, str1 ends with "world!", so the result is true. str2 does not end with "world!", so the result is false.

3. Combining ^ and $ to Match an Exact String

When you use both ^ and $ together, the regular expression ensures that the entire string exactly matches the pattern. This is useful for validating if a string is exactly equal to a certain value.

Example: Matching an Exact String

let pattern = /^Hello, World!$/; let str1 = "Hello, World!"; let str2 = "Hello, World! "; console.log(pattern.test(str1)); // true (exact match) console.log(pattern.test(str2)); // false (extra space at the end)
  • ^Hello, World!$: Matches the string "Hello, World!" exactly (no extra characters before or after).

In this example, str1 matches exactly, while str2 has an extra space, which causes the match to fail.

4. Practical Use Cases

Here are some common use cases where anchors are helpful:

a) Validating Email Address Prefix

You can use the ^ anchor to check if an email address starts with a certain string, like user.

let pattern = /^user/; let email = "user@example.com"; console.log(pattern.test(email)); // true
  • ^user: Matches any string starting with "user".

b) Validating URL Protocol

You can use the ^ anchor to check if a URL starts with http:// or https://.

let pattern = /^https?:\/\//; let url1 = "https://example.com"; let url2 = "ftp://example.com"; console.log(pattern.test(url1)); // true (starts with "https://") console.log(pattern.test(url2)); // false (starts with "ftp://")
  • ^https?:\/\/: Matches any string starting with http:// or https:// (due to the optional s in https?).

c) Ensuring String Ends with Specific Word

You can use the $ anchor to check if a string ends with a specific word, such as "done".

let pattern = /done$/; let sentence1 = "Task completed, done"; let sentence2 = "Task done, completed"; console.log(pattern.test(sentence1)); // true (ends with "done") console.log(pattern.test(sentence2)); // false (does not end with "done")
  • done$: Matches any string that ends with the word "done".

5. Anchors with Other Patterns

You can combine ^ and $ with other regular expression patterns to match more complex scenarios.

a) Match Strings that Start with a Number and End with a Period

let pattern = /^[0-9].*\.$/; let str1 = "1. This is a valid string."; let str2 = "This is not a valid string."; console.log(pattern.test(str1)); // true console.log(pattern.test(str2)); // false
  • ^[0-9]: Ensures the string starts with a digit.
  • .*\.$: Ensures the string ends with a period.

b) Validate a String of Digits Only

You can use both ^ and $ to validate that a string contains only digits.

let pattern = /^\d+$/; let str1 = "12345"; let str2 = "123a45"; console.log(pattern.test(str1)); // true (only digits) console.log(pattern.test(str2)); // false (contains "a")
  • ^\d+$: Ensures the entire string consists of one or more digits.

Conclusion

  • ^ (caret) is used to match the beginning of a string.
  • $ (dollar sign) is used to match the end of a string.
  • When both are used together, ^ and $ ensure that the entire string exactly matches the pattern.

These anchors help make your regular expressions more precise by restricting where the pattern can appear within a string. They are especially useful in tasks like validating inputs or checking for specific formats, such as email addresses, URLs, or phone numbers.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close