Friday, March 29, 2024
HomeWeb developmentFast Tip: Testing if a String Matches a Regex in JavaScript

Fast Tip: Testing if a String Matches a Regex in JavaScript


On this transient tutorial on JavaScript regex matching, you’ll discover ways to take a look at whether or not a string matches an everyday expression utilizing the take a look at() methodology.

Strings are items of textual content that may include quite a lot of information — resembling URLs, cellphone numbers, names, numbers, and extra. In lots of instances, it’s good to verify whether or not or not a string incorporates a bit of textual content or sure kinds of characters.

Once you’re testing if a string incorporates a selected substring, you could be inclined to make use of a way like indexOf(). Nonetheless, for extra versatile testing and circumstances, utilizing common expressions is a greater choice.

JavaScript regex matching lets you verify if a string incorporates a selected sample, substring, or kinds of characters. Common expressions are helpful for detecting info in a string that may be written in several codecs, resembling dates.

Testing Strings towards Common Expressions

To check whether or not a string matches an everyday expression, you need to first create an everyday expression occasion. Then, you should utilize the take a look at() methodology out there on the common expression to verify if the string matches the common expression or not.

The take a look at() methodology accepts one parameter: the string to check towards the sample. It returns a Boolean worth indicating whether or not the string matches the common expression or not.

For instance:

const sample = /take a look at.*common/;
const str = 'I wish to take a look at this string towards an everyday expression';
if (sample.take a look at(str)) {
  console.log('Matched');
} else {
  console.log('Not Matched');
}

On this instance, you create the sample take a look at.*common. This sample signifies that a string should include the phrases take a look at and common in that order, and that these phrases will be separated by zero or extra occurrences of any character.

If take a look at() returns true, Matched is logged within the console. In any other case, Not Matched is logged within the console.

Since str incorporates the phrases take a look at and common, and take a look at precedes common within the string, it’s going to match towards the sample and take a look at() will return true.

You may also use the RegExp constructor to declare the patterns:

const sample = new RegExp('take a look at.*common');
const str = 'I wish to take a look at this string towards an everyday expression';
if (sample.take a look at(str)) {
  console.log('Matched');
} else {
  console.log('Not Matched');
}

You possibly can take a look at this out within the following CodePen demo.

See the Pen
Testing a String In opposition to a Common Expression
by SitePoint (@SitePoint)
on CodePen.

Frequent Examples

This part reveals some examples of use JavaScript regex matching to check frequent use instances. It ought to be famous that the common expressions right here may not be the right resolution in every case. They’re every used to provide a easy instance of how the method works.

Testing URLs

You possibly can take a look at if a string is a URL utilizing common expressions. You possibly can experiment with this utilizing the next CodePen demo.

See the Pen
Take a look at if a String is a URL in JavaScript
by SitePoint (@SitePoint)
on CodePen.

Please notice that the common expression sample used above expects the URL to start with http:// or https://.

Testing Emails

You possibly can take a look at if a string is a sound e mail tackle utilizing common expressions. The next CodePen demo reveals how.

See the Pen
Take a look at is a String is an E mail in JS
by SitePoint (@SitePoint)
on CodePen.

Testing Dates

You possibly can take a look at if a string is a date utilizing common expressions. The next CodePen demo reveals how it may be performed.

See the Pen
Take a look at if a String is a date in JavaScript
by SitePoint (@SitePoint)
on CodePen.

Please notice that the common expression sample used above expects the date to be of the codecs “DD-MM-YYYY” or “DD/MM/YYYY”.

Different Strategies for JavaScript Regex Matching

There are different strategies to check whether or not a string matches an everyday expression. This text doesn’t cowl all of them, however right here’s a short overview of them:

  • match. This methodology is offered on strings. It accepts an everyday expression as a parameter and retrieves the elements of the string that match the common expression, if there are any.
  • search. This methodology is offered on strings. It accepts an everyday expression as a parameter, searches if the common expression sample exists within the string, and retrieves the index of the primary incidence of the sample within the string if it exists.
  • exec. This methodology is offered on common expressions. It accepts a string as a parameter, searches for the common expression sample within the string, and retrieves the outcomes, if there are any.

Conclusion

Common expressions are very helpful for testing if a string incorporates a sure sample or substring. With JavaScript regex matching, you possibly can verify if a string is a URL, a date, an IP tackle, or different varieties and codecs.

In comparison with utilizing different strategies like indexOf(), the take a look at() methodology that’s out there on common expressions offers you extra flexibility when testing whether or not a string matches a sample or not.

Associated studying:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments