Skip to content

matches

pattern

Signature

js
pattern(value, pattern)

Description

Check if a string value matches a regular expression pattern

Parameters

NameTypeRequiredDescription
value*yesThe value to test
patternRegExpyesThe regular expression pattern to match against

Returns

TypeDescription
booleanTrue if the value is a string and matches the pattern

Examples

javascript
matches.pattern('hello123', /\d+/) // true
matches.pattern('hello', /^\w+$/) // true
matches.pattern('test@example.com', /^[\w.-]+@[\w.-]+\.\w+$/) // true
matches.pattern('hello', /\d+/) // false
matches.pattern(123, /\d+/) // false (not a string)
matches.pattern('hello', 'pattern') // false (not a RegExp)