Categories
JavaScript Answers

How to match exact string with JavaScript?

Spread the love

Sometimes, we want to match exact string with JavaScript.

In this article, we’ll look at how to match exact string with JavaScript.

How to match exact string with JavaScript?

To match exact string with JavaScript, we use the ^ and $ characters in the regex.

For instance, we write

const r = /^a$/;

to create the regex r that matches ‘a’ exactly.

^ means the start of the string and $ means the end of the string.

Conclusion

To match exact string with JavaScript, we use the ^ and $ characters in the regex.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.