RegExp-Methods Flashcards
.exec
.exec(str contents)
=> {arr-obj|null} null if no matches found, else, returns array-like object:
{0: match, 1+: capture-parens, index: index-of-match, input: string-contents}
#safe
#examples
- w/o loop:
/d(a)(v?)/.exec(“david dave dad”) => [“dav”, “a”, “v”, index: 0, input: “david dave dad”]
- w/ loop:
// MUST have regexp outside assigned to variable prior to loop, else loop infinity
// also needs g flag
var r = /d(a)(v?)/g;
var contents = ‘david dave dad’;
while (var match = r.exec(contents)) { … } =>
[“dav”, “a”, “v”, index: 0, input: “david dave dad”]
[“dav”, “a”, “v”, index: 6, input: “david dave dad”]
[“dav”, “a”, “”, index: 11, input: “david dave dad”]
.test
.test(str contents) => {bool} test results #safe #fastest
.toString
.toString()
=> {str} string representation of RegExp
#safe
#example
/r/gi.toString() => “/r/gi”
#note—if ‘r’ is all you’re interested in, use .source property