Permalink
Browse files

comments, tests

  • Loading branch information...
slevithan committed Apr 23, 2012
1 parent 39e78aa commit b4942bc071bda26804a28499f82791db69e08767
Showing with 49 additions and 46 deletions.
  1. +29 −29 README.md
  2. +8 −8 src/addons/build.js
  3. +4 −1 tests/tests.js
  4. +8 −8 xregexp-all.js
View
@@ -108,6 +108,35 @@ XRegExp('^[\\p{Latin}\\p{Common}]+$').test('Über Café.'); // -> true
XRegExp uses Unicode 6.1's Basic Multilingual Plane.
+## XRegExp.build
+
+First include the script:
+
+~~~ html
+<script src="xregexp.js"></script>
+<script src="addons/build.js"></script>
+~~~
+
+You can then build regular expressions using named subpatterns, for readability and pattern reuse:
+
+~~~ js
+var time = XRegExp.build('(?x)^ {{hours}} ({{minutes}}) $', {
+ hours: XRegExp.build('{{h12}} : | {{h24}}', {
+ h12: /1[0-2]|0?[1-9]/,
+ h24: /2[0-3]|[01][0-9]/
+ }, 'x'),
+ minutes: /^[0-5][0-9]$/
+});
+
+time.test('10:59'); // -> true
+XRegExp.exec('10:59', time).minutes; // -> '59'
+~~~
+
+Named subpatterns can be provided as strings or regex objects. A leading `^` and trailing unescaped `$` are stripped from subpatterns if both are present, which allows embedding independently useful anchored patterns. `{{…}}` tokens can be quantified as a single unit. Backreferences in the outer pattern and provided subpatterns are automatically renumbered to work correctly within the larger combined pattern. The syntax `({{name}})` works as shorthand for named capture via `(?<name>{{name}})`.
+
+See also: *[Creating Grammatical Regexes Using XRegExp.build](http://blog.stevenlevithan.com/archives/grammatical-patterns-xregexp-build)*.
+
+
## XRegExp.matchRecursive
First include the script:
@@ -159,35 +188,6 @@ XRegExp.matchRecursive(str, '<', '>', 'gy');
`XRegExp.matchRecursive` throws an error if it sees an unbalanced delimiter in the target string.
-## XRegExp.build
-
-First include the script:
-
-~~~ html
-<script src="xregexp.js"></script>
-<script src="addons/build.js"></script>
-~~~
-
-You can then build regular expressions using named subpatterns, for readability and pattern reuse:
-
-~~~ js
-var time = XRegExp.build('(?x)^ {{hours}} : ({{minutes}}) $', {
- hours: XRegExp.build('{{h12}} | {{h24}}', {
- h12: /1[0-2]|0?[1-9]/,
- h24: /2[0-3]|[01]?[0-9]/
- }, 'x'),
- minutes: /^[0-5]?[0-9]$/
-});
-
-time.test('23:59'); // -> true
-XRegExp.exec('23:59', time).minutes; // -> '59'
-~~~
-
-Named subpatterns can be provided as strings or regex objects. A leading `^` and trailing unescaped `$` are stripped from subpatterns if both are present, which allows embedding independently useful anchored patterns. `{{…}}` tokens can be quantified as a single unit. Backreferences in the outer pattern and provided subpatterns are automatically renumbered to work correctly within the larger combined pattern. The syntax `({{name}})` works as shorthand for named capture via `(?<name>{{name}})`.
-
-See also: *[Creating Grammatical Regexes Using XRegExp.build](http://blog.stevenlevithan.com/archives/grammatical-patterns-xregexp-build)*.
-
-
## XRegExp Prototype Methods
Include the script:
View
@@ -51,19 +51,19 @@
* @returns {RegExp} Regex with interpolated subpatterns.
* @example
*
- * var time = XRegExp.build('(?x)^ {{hours}} : ({{minutes}}) $', {
- * hours: XRegExp.build('{{h12}} | {{h24}}', {
+ * var time = XRegExp.build('(?x)^ {{hours}} ({{minutes}}) $', {
+ * hours: XRegExp.build('{{h12}} : | {{h24}}', {
* h12: /1[0-2]|0?[1-9]/,
- * h24: /2[0-3]|[01]?[0-9]/
+ * h24: /2[0-3]|[01][0-9]/
* }, 'x'),
- * minutes: /^[0-5]?[0-9]$/
+ * minutes: /^[0-5][0-9]$/
* });
- * time.test('23:59'); // -> true
- * XRegExp.exec('23:59', time).minutes; // -> '59'
+ * time.test('10:59'); // -> true
+ * XRegExp.exec('10:59', time).minutes; // -> '59'
*/
XRegExp.build = function (pattern, subs, flags) {
var data = {},
- numCaps = 0, // Caps is an abbr for captures
+ numCaps = 0, // Caps is short for captures
numPriorCaps,
numOuterCaps = 0,
outerCapsMap = [0],
@@ -108,7 +108,7 @@
if (paren) { // Capturing group
capName = data[subName].names[numCaps - numPriorCaps];
++numCaps;
- if (capName) { // If the current capture has a name
+ if (capName) { // If the current capture has a name, preserve the name
return "(?<" + capName + ">";
}
} else if (backref) { // Backreference
View
@@ -305,7 +305,10 @@ test("XRegExp.uninstall", function () {
});
test("XRegExp.union", function () {
- expect(0);
+ equal(XRegExp.union([XRegExp("(?<a>a)\\k<a>")], "n").test("aa"), true, "Apply flag n (test 1)");
+ raises(function () {XRegExp.union([XRegExp("(?<a>a)\\k<a>"), /(b)\1/], "n");}, SyntaxError, "Apply flag n (test 2)");
+ raises(function () {XRegExp.union([XRegExp("(?<a>a)\\k<a>"), /(b)\1/, XRegExp("(?<x>)")], "n");}, SyntaxError, "Apply flag n (test 3)");
+
// TODO: Add tests
});
View
@@ -2110,19 +2110,19 @@ XRegExp = XRegExp || (function (undef) {
* @returns {RegExp} Regex with interpolated subpatterns.
* @example
*
- * var time = XRegExp.build('(?x)^ {{hours}} : ({{minutes}}) $', {
- * hours: XRegExp.build('{{h12}} | {{h24}}', {
+ * var time = XRegExp.build('(?x)^ {{hours}} ({{minutes}}) $', {
+ * hours: XRegExp.build('{{h12}} : | {{h24}}', {
* h12: /1[0-2]|0?[1-9]/,
- * h24: /2[0-3]|[01]?[0-9]/
+ * h24: /2[0-3]|[01][0-9]/
* }, 'x'),
- * minutes: /^[0-5]?[0-9]$/
+ * minutes: /^[0-5][0-9]$/
* });
- * time.test('23:59'); // -> true
- * XRegExp.exec('23:59', time).minutes; // -> '59'
+ * time.test('10:59'); // -> true
+ * XRegExp.exec('10:59', time).minutes; // -> '59'
*/
XRegExp.build = function (pattern, subs, flags) {
var data = {},
- numCaps = 0, // Caps is an abbr for captures
+ numCaps = 0, // Caps is short for captures
numPriorCaps,
numOuterCaps = 0,
outerCapsMap = [0],
@@ -2167,7 +2167,7 @@ XRegExp = XRegExp || (function (undef) {
if (paren) { // Capturing group
capName = data[subName].names[numCaps - numPriorCaps];
++numCaps;
- if (capName) { // If the current capture has a name
+ if (capName) { // If the current capture has a name, preserve the name
return "(?<" + capName + ">";
}
} else if (backref) { // Backreference

0 comments on commit b4942bc

Please sign in to comment.