JS: String.prototype.match
str.match(findStr)-
The string findStr is converted to regex first by RegExp, then
str.match(regex_obj)is called. str.match(regex_obj)-
Return a Iterable or null if no match.
- If regex_obj has regex flag
g, the result is all occurences. - Else, the result is all captured groups of first matched string. (and if the regex does not contain any capture, it return first matched string).
Return Value has flag g no flag g capture group all occurrences captured groups in first matched string no capture group all occurrences first matched string (array of 1 item) In result array, index 0 is the whole matched string, index 1 is 1st captured group or occurrence, index 2 is the 2nd, etc.
The result array have these properties (their values are undefined if no g flag):
index→ The beginning position of first occurrence.input→ The input string.groups→ (new in ECMAScript 2018) A object. Key is name of named capture group. Value is corresponding substring, orundefinedif there is none.
- If regex_obj has regex flag
Example
Get all occurrences
/* get all occurrences of jpg file names */ const xtext = ` cat.jpg dog.jpg house.jpg `; // use flag global const xresult = xtext.match(/[\w]+.jpg/g); console.log(xresult); // [ "cat.jpg", "dog.jpg", "house.jpg" ]
Get all captures of first match
/* get all captures of first match */ const xtext = ` src="hhkb_keyboard.jpg" alt="hhkb" src="kinesis_360_keyboard.jpg" alt="kinesis_360" src="glove_80_keyboard.jpg" alt="glove_80" src="uhk_80_keyboard.jpg" alt="uhk_80" `; // no flag global const xresult = xtext.match(/src="([^"]+)" alt="([^"]+)"/); console.log(xresult); /* [ 'src="hhkb_keyboard.jpg" alt="hhkb"', "hhkb_keyboard.jpg", "hhkb", index: 1, input: "\n" + 'src="hhkb_keyboard.jpg" alt="hhkb"\n' + 'src="kinesis_360_keyboard.jpg" alt="kinesis_360"\n' + 'src="glove_80_keyboard.jpg" alt="glove_80"\n' + 'src="uhk_80_keyboard.jpg" alt="uhk_80"\n', groups: undefined ] */
Get just first match
/* get just first occurrence */ const xtext = ` src="hhkb_keyboard.jpg" alt="hhkb" src="kinesis_360_keyboard.jpg" alt="kinesis_360" src="glove_80_keyboard.jpg" alt="glove_80" src="uhk_80_keyboard.jpg" alt="uhk_80" `; // no flag global, no capture const xresult = xtext.match(/src="[^"]+" alt="[^"]+"/); console.log(xresult); /* [ 'src="hhkb_keyboard.jpg" alt="hhkb"', index: 1, input: "\n" + 'src="hhkb_keyboard.jpg" alt="hhkb"\n' + 'src="kinesis_360_keyboard.jpg" alt="kinesis_360"\n' + 'src="glove_80_keyboard.jpg" alt="glove_80"\n' + 'src="uhk_80_keyboard.jpg" alt="uhk_80"\n', groups: undefined ] */
JavaScript. Regular Expression
- JS: Regular Expression Tutorial
- JS: Regular Expression Functions
- JS: Create Regex Object
- JS: Regular Expression Syntax
- JS: Regular Expression Flags
- JS: Regex Replace String Dollar Sign
- JS: Regex Replace Function Args
- JS: RegExp (class)
- JS: RegExp Constructor
- JS: RegExp.prototype
- JS: String.prototype.search
- JS: String.prototype.match
- JS: String.prototype.matchAll
- JS: String.prototype.replace
- JS: String.prototype.replaceAll
- JS: RegExp.prototype.test
- JS: RegExp.prototype.exec
JS String.prototype
- JS: String.prototype.constructor
- JS: String.prototype.length
- JS: String.prototype.at (Extract Char at Index)
- JS: String.fromCharCode (Char ID to Char) ❌
- JS: String.prototype.concat
- JS: String.prototype.repeat
- JS: String.prototype.trim
- JS: String.prototype.trimStart
- JS: String.prototype.trimEnd
- JS: String.prototype.padStart
- JS: String.prototype.padEnd
- JS: String.prototype.slice
- JS: String.prototype.substring ❌
- JS: String.prototype.substr
- JS: String.prototype.indexOf
- JS: String.prototype.lastIndexOf
- JS: String.prototype.includes
- JS: String.prototype.startsWith
- JS: String.prototype.endsWith
- JS: String.prototype.search
- JS: String.prototype.match
- JS: String.prototype.matchAll
- JS: String.prototype.replace
- JS: String.prototype.split
- JS: String.prototype.toLowerCase
- JS: String.prototype.charAt (Extract Char at Index) ❌
- JS: String.prototype.charCodeAt (Char to Char ID) ❌
- JS: String.prototype.codePointAt (Char to Char ID) ❌