The _.isMatch
function from the Underscore.js library is typically used to check if an object has the same properties and values as the source. However, the function can also be used to check if a string matches a regular expression pattern.
Here's an example code that demonstrates how to use _.isMatch
function to test if a string matches a pattern:
index.tsx303 chars11 lines
In this example, we're using a regular expression pattern /^[A-Z][a-z]+\s[A-Z][a-z]+$/
to validate that fullName
is a string consisting of two words with the first letter of each word capitalized. We're passing fullName
and pattern
as arguments to the _.isMatch
function. The function returns true
if the string matches the pattern, and false
otherwise.
In the example above, fullName
matches the pattern, so the output will be: John Doe matches the pattern.
gistlibby LogSnag