string.prototype.match
и regex.prototype.test
.
'string'.match (/ regex /):
let a = 'hello'.match(/^[gh]/); // truthy (['h'])
let b = 'gello'.match(/^[gh]/); // truthy (['g'])
let c = 'ello'.match(/^[gh]/); // falsey (null)
console.log(a, b, c);
/ regex / .test ('string'):
let a = /^[gh]/.test('hello'); // true
let b = /^[gh]/.test('gello'); // true
let c = /^[gh]/.test('ello'); // false
console.log(a, b, c);
Символ регулярного выражения ^
обеспечивает совпадение регулярного выражения только в начале строки.