Created
April 8, 2011 11:12
-
-
Save insin/909651 to your computer and use it in GitHub Desktop.
findAll method for RegExps with results in the vein of Python's re.findall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RegExp.prototype.findAll = function(str) { | |
var match = null, results = []; | |
while ((match = this.exec(str)) !== null) { | |
switch (match.length) { | |
case 1: | |
results[results.length] = match[0]; | |
break; | |
case 2: | |
results[results.length] = match[1]; | |
break; | |
default: | |
results[results.length] = match.slice(1); | |
} | |
if (!this.global) { | |
break; | |
} | |
} | |
return results; | |
} | |
RegExp.findAll = function(re, str, flags) { | |
if (Object.prototype.toString.call(re) != '[object RegExp]') { | |
re = new RegExp(re, flags); | |
} | |
return re.findAll(str); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<html lang="en"> | |
<head> | |
<title>RegExp-findAll QUnit Tests</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<meta name="Author" content="Jonathan Buchanan"> | |
<!-- Test framework --> | |
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen"> | |
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script> | |
<!-- Code under test --> | |
<script type="text/javascript" src="RegExp-findall.js"></script> | |
<!-- Test cases --> | |
<script type="text/javascript" src="tests.js"></script> | |
</head> | |
<body> | |
<h1 id="qunit-header">RegExp-findAll QUnit Tests</h1> | |
<h2 id="qunit-banner"></h2> | |
<div id="qunit-testrunner-toolbar"></div> | |
<h2 id="qunit-userAgent"></h2> | |
<ol id="qunit-tests"></ol> | |
<div id="qunit-fixture"></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module = QUnit.module; | |
module('RegExp-findAll'); | |
(function() { | |
var str = 'abc123def456'; | |
var nomatch = '!"£$%^&*()'; | |
test('RegExp.prototype.findAll', 12, function() { | |
deepEqual(/[a-z]\d/.findAll(str), ['c1']); | |
deepEqual(/[a-z]\d/g.findAll(str), ['c1', 'f4']); | |
deepEqual(/[a-z](\d)/.findAll(str), ['1']); | |
deepEqual(/[a-z](\d)/g.findAll(str), ['1', '4']); | |
deepEqual(/[a-z](\d)(\d)/.findAll(str), [['1', '2']]); | |
deepEqual(/[a-z](\d)(\d)/g.findAll(str), [['1', '2'], ['4', '5']]); | |
deepEqual(/[a-z]\d/.findAll(nomatch), []); | |
deepEqual(/[a-z]\d/g.findAll(nomatch), []); | |
deepEqual(/[a-z](\d)/.findAll(nomatch), []); | |
deepEqual(/[a-z](\d)/g.findAll(nomatch), []); | |
deepEqual(/[a-z](\d)(\d)/.findAll(nomatch), []); | |
deepEqual(/[a-z](\d)(\d)/g.findAll(nomatch), []); | |
}); | |
test('RegExp.findAll', 12, function() { | |
deepEqual(RegExp.findAll('[a-z]\\d', str), ['c1']); | |
deepEqual(RegExp.findAll('[a-z]\\d', str, 'g'), ['c1', 'f4']); | |
deepEqual(RegExp.findAll('[a-z](\\d)', str), ['1']); | |
deepEqual(RegExp.findAll('[a-z](\\d)', str, 'g'), ['1', '4']); | |
deepEqual(RegExp.findAll('[a-z](\\d)(\\d)', str), [['1', '2']]); | |
deepEqual(RegExp.findAll('[a-z](\\d)(\\d)', str, 'g'), [['1', '2'], ['4', '5']]); | |
deepEqual(RegExp.findAll('[a-z]\\d', nomatch), []); | |
deepEqual(RegExp.findAll('[a-z]\\d', nomatch, 'g'), []); | |
deepEqual(RegExp.findAll('[a-z](\\d)', nomatch), []); | |
deepEqual(RegExp.findAll('[a-z](\\d)', nomatch, 'g'), []); | |
deepEqual(RegExp.findAll('[a-z](\\d)(\\d)', nomatch), []); | |
deepEqual(RegExp.findAll('[a-z](\\d)(\\d)', nomatch, 'g'), []); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment