Methods of RegExp and String | Regex all match
Inthisarticlewe’llcovervariousmethodsthatworkwithregexpsin-depth.Themethodstr.match(regexp)findsmatchesforregexpinthestringstr.Ithas3modes:Iftheregexpdoesn’thaveflagg,thenitreturnsthefirstmatchasanarraywithcapturinggroupsandpropertiesindex(positionofthematch),input(inputstring,equalsstr):letstr="IloveJavaScript";letresult=str.match(/Java(Script)/);alert(result[0]);//JavaScript(fullmatch)alert(result[1]);//Script(firstcapturinggroup)alert(result.length);//2//Additionalinformation:alert(result...
In this article we’ll cover various methods that work with regexps in-depth.
The method str.match(regexp) finds matches for regexp in the string str.
It has 3 modes:
If the regexp doesn’t have flag g, then it returns the first match as an array with capturing groups and properties index (position of the match), input (input string, equals str):
let str = "I love JavaScript"; let result = str.match(/Java(Script)/); alert( result[0] ); // JavaScript (full match) alert( result[1] ); // Script (first capturing group) alert( result.length ); // 2 // Additional information: alert( result.index ); // 7 (match position) alert( result.input ); // I love JavaScript (source string)If the regexp has flag g, then it returns an array of all matches as strings, without capturing groups and other details.
let str = "I love JavaScript...