how to use the match function in javascript for regex

Solutions on MaxInterview for how to use the match function in javascript for regex by the best coders in the world

showing results for - "how to use the match function in javascript for regex"
Alex
30 Jan 2020
1const str = 'For more information, see Chapter 3.4.5.1';
2const re = /see (chapter \d+(\.\d)*)/i;
3const found = str.match(re);
4
5console.log(found);
6
7// logs [ 'see Chapter 3.4.5.1',
8//        'Chapter 3.4.5.1',
9//        '.1',
10//        index: 22,
11//        input: 'For more information, see Chapter 3.4.5.1' ]
12
13// 'see Chapter 3.4.5.1' is the whole match.
14// 'Chapter 3.4.5.1' was captured by '(chapter \d+(\.\d)*)'.
15// '.1' was the last value captured by '(\.\d)'.
16// The 'index' property (22) is the zero-based index of the whole match.
17// The 'input' property is the original string that was parsed.