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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *