๐str.match()
๐ replit
// first match โด
// 0 1 2 3 4
// index: 01234567890123456789012345678901234567890
// โญโโ first match โโโฎ
const found = 'For more information, see Chapter 3.4.5.1'.match(
// โฐโโโ group โโโโฏ
/see (chapter \d+(\.\d)*)/i // ignore case, non-global
);
// โญ๏ธ three modes of str.match(regex) method's return value
log(found);
// โญ๏ธ 2. non-gloabl mode:
// (return array of informations about first match)
// [
// 'see Chapter 3.4.5.1', // whole match
// 'Chapter 3.4.5.1', // first capture group โญ๏ธ
// '.1', // last value captured by '(\.\d)'
// index: 22, // index of the whole match
// input: 'For more information, see Chapter 3.4.5.1', // original string
// groups: undefined // object of named capturing groups
// ]
let str = "We will, we will rock you";
// โญ๏ธ 1. global mode:
// (return array of all matches, no capturing groups)
str.match(/we/gi), // [ 'We', 'we' ]
// โญ๏ธ 3. no match:
// (return null)
str.match(/they/), // null โญ๏ธ
Last updated
Was this helpful?