RegExp-Methods Flashcards

1
Q

.exec

A

.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”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

.test

A
.test(str contents)
=>  {bool} test results
#safe
#fastest
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

.toString

A

.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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly