Check whether a string matches a regex in JS Check whether a string matches a regex in JS javascript javascript

Check whether a string matches a regex in JS


Use regex.test() if all you want is a boolean result:

console.log(/^([a-z0-9]{5,})$/.test('abc1')); // falseconsole.log(/^([a-z0-9]{5,})$/.test('abc12')); // trueconsole.log(/^([a-z0-9]{5,})$/.test('abc123')); // true