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
^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
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
^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
.
^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://
.
^https?:\/\/
: Matches any string starting withhttp://
orhttps://
(due to the optionals
inhttps?
).
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".
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
^[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.
^\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.