Skip to content

Commit

Permalink
add new .build addon (thanks to @LeaVerou)
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Mar 30, 2012
1 parent db97d63 commit 47b338d
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 105 deletions.
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ unicodeWord.test('日本語'); // -> true
unicodeWord.test('العربية'); // -> true
~~~

The base script adds `\p{L}` (and its alias, `\p{Letter}`), but other Unicode categories, scripts, blocks, and properties require addon packages. Try these next examples after additionally including `unicode-scripts.js`:
The base script adds `\p{Letter}` and its alias `\p{L}`, but other Unicode categories, scripts, blocks, and properties require addon packages. Try these next examples after additionally including `unicode-scripts.js`:

~~~ js
XRegExp('^\\p{Hiragana}+$').test('ひらがな'); // -> true
Expand All @@ -109,9 +109,9 @@ XRegExp uses the Unicode 6.1 character database (released January 2012).
More details: [Addons: Unicode](http://xregexp.com/plugins/#unicode).


## XRegExp Match Recursive
## XRegExp.matchRecursive

First include the Match Recursive script:
First include the script:

~~~ html
<script src="xregexp.js"></script>
Expand Down Expand Up @@ -156,6 +156,30 @@ XRegExp.matchRecursive(str, '<', '>', 'gy');
More details: [Addons: Match Recursive](http://xregexp.com/plugins/#matchRecursive).


## XRegExp.build

First include the script:

~~~ html
<script src="xregexp.js"></script>
<script src="addons/build.js"></script>
~~~

You can then build complex regular expressions that use named subpatterns for readability and code reuse:

~~~ js
var color = XRegExp.build("{{keyword}}|{{func}}|{{hex}}", {
keyword: /^(?:red|tan|[a-z]{4,20})$/,
func: XRegExp.build("^(?:rgb|hsl)a?\\((?:\\s*{{number}}%?\\s*,?\\s*){3,4}\\)$", {
number: /^-?\d+(?:\.\d+)?$/
}),
hex: /^#(?:[0-9a-f]{1,2}){3}$/
});
~~~

The `{{…}}` syntax works only for regexes created by `XRegExp.build`.


## How to run server-side tests

~~~ bash
Expand All @@ -176,6 +200,8 @@ XRegExp and addons copyright 2007-2012 by [Steven Levithan](http://stevenlevitha

Tools: Unicode range generators by [Mathias Bynens](http://mathiasbynens.be/). Source file concatenator by [Bjarke Walling](http://twitter.com/walling).

Thanks to [Lea Verou](http://lea.verou.me/) for the [inspiration](http://lea.verou.me/2011/03/create-complex-regexps-more-easily/) behind XRegExp.build.

All code released under the [MIT License](http://opensource.org/licenses/mit-license.php).

Fork me to show support, fix, and extend.
Expand Down
66 changes: 66 additions & 0 deletions src/addons/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*!
* XRegExp.build v0.1.0-beta
* Copyright 2012 Steven Levithan <http://xregexp.com/>
* Available under the MIT License
* Based on RegExp.create by Lea Verou <http://lea.verou.me/2011/03/create-complex-regexps-more-easily/>
*/

(function (XRegExp) {
"use strict";

var data = null;

XRegExp.install("extensibility");

/**
* Builds complex regular expressions using named subpatterns for readability and code reuse.
* @memberOf XRegExp
* @param {String} pattern XRegExp pattern using `{{..}}` for embedded subpatterns.
* @param {Object} subs Named subpatterns as strings or regexes. Leading ^ and trailing $ are
* stripped from subpatterns provided as regex objects.
* @param {String} [flags] Any combination of XRegExp flags.
* @returns {RegExp} Extended regular expression object.
* @example
*
* var color = XRegExp.build("{{keyword}}|{{func}}|{{hex}}", {
* keyword: /^(?:red|tan|[a-z]{4,20})$/,
* func: XRegExp.build("^(?:rgb|hsl)a?\\((?:\\s*{{number}}%?\\s*,?\\s*){3,4}\\)$", {
* number: /^-?\d+(?:\.\d+)?$/
* }),
* hex: /^#(?:[0-9a-f]{1,2}){3}$/
* });
*/
XRegExp.build = function (pattern, subs, flags) {
var p, regex;
data = {};
for (p in subs) {
data[p] = XRegExp.isRegExp(subs[p]) ? subs[p].source.replace(/^\^|\$$/g, "") : subs[p];
}
try {
regex = XRegExp(pattern, flags);
} catch (err) {
throw err;
} finally {
data = null; // Reset
}
return regex;
};

XRegExp.addToken(
/{{([\w$]+)}}/,
function (match) {
if (data[match[1]] === undefined) {
throw new ReferenceError("unknown property: " + match[1]);
}
return "(?:" + data[match[1]] + ")";
},
{
trigger: function () {
return !!data;
},
scope: "all"
}
);

}(XRegExp));

6 changes: 3 additions & 3 deletions src/addons/matchrecursive.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* XRegExp Match Recursive v0.2.0-beta
* XRegExp.matchRecursive v0.2.0-beta
* Copyright 2009-2012 Steven Levithan <http://xregexp.com/>
* Available under the MIT License
*/
Expand All @@ -12,8 +12,8 @@
* position data. An error is thrown if delimiters are unbalanced within the data.
* @memberOf XRegExp
* @param {String} str String to search.
* @param {String} left Left delimiter as an XRegExp pattern string.
* @param {String} right Right delimiter as an XRegExp pattern string.
* @param {String} left Left delimiter as an XRegExp pattern.
* @param {String} right Right delimiter as an XRegExp pattern.
* @param {String} [flags] Flags for the left and right delimiters. Use any of: `gimnsxy`.
* @param {Object} [options] Lets you specify `valueNames` and `escapeChar` options.
* @returns {Array} Array of matches, or an empty array.
Expand Down
15 changes: 4 additions & 11 deletions src/addons/unicode/unicode-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
(function (XRegExp) {
"use strict";

var unicode = {},
extensible = XRegExp.isInstalled("extensibility");
var unicode = {};

/*--------------------------------------
* Private helper functions
Expand Down Expand Up @@ -83,9 +82,7 @@
* Core functionality
*------------------------------------*/

if (!extensible) {
XRegExp.install("extensibility"); // Temporarily install
}
XRegExp.install("extensibility");

// Adds Unicode token syntax to XRegExp: \p{..} or \P{..}
XRegExp.addToken(
Expand Down Expand Up @@ -116,9 +113,9 @@
* @example
*
* XRegExp.addUnicodePackage({
* xdigit: '0030-00390041-00460061-0066' // 0-9A-Fa-f
* XDigit: '0030-00390041-00460061-0066' // 0-9A-Fa-f
* }, {
* xdigit: 'Hexadecimal'
* XDigit: 'Hexadecimal'
* });
*/
XRegExp.addUnicodePackage = function (pack, aliases) {
Expand Down Expand Up @@ -149,9 +146,5 @@
L: "Letter"
});

if (!extensible) {
XRegExp.uninstall("extensibility"); // Revert to previous state
}

}(XRegExp));

10 changes: 1 addition & 9 deletions src/addons/unicode/unicode-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
throw new ReferenceError("Unicode Base must be loaded before Unicode Blocks");
}

var extensible = XRegExp.isInstalled("extensibility");

if (!extensible) {
XRegExp.install("extensibility"); // Temporarily install
}
XRegExp.install("extensibility");

XRegExp.addUnicodePackage({
InBasic_Latin: "0000-007F",
Expand Down Expand Up @@ -183,9 +179,5 @@
InSpecials: "FFF0-FFFF"
});

if (!extensible) {
XRegExp.uninstall("extensibility"); // Revert to previous state
}

}(XRegExp));

10 changes: 1 addition & 9 deletions src/addons/unicode/unicode-categories.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions src/addons/unicode/unicode-properties.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions src/addons/unicode/unicode-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
throw new ReferenceError("Unicode Base must be loaded before Unicode Scripts");
}

var extensible = XRegExp.isInstalled("extensibility");

if (!extensible) {
XRegExp.install("extensibility"); // Temporarily install
}
XRegExp.install("extensibility");

XRegExp.addUnicodePackage({
Arabic: "0600-06040606-060B060D-061A061E0620-063F0641-064A0656-065E066A-066F0671-06DC06DE-06FF0750-077F08A008A2-08AC08E4-08FEFB50-FBC1FBD3-FD3DFD50-FD8FFD92-FDC7FDF0-FDFCFE70-FE74FE76-FEFC",
Expand Down Expand Up @@ -98,9 +94,5 @@
Yi: "A000-A48CA490-A4C6"
});

if (!extensible) {
XRegExp.uninstall("extensibility"); // Revert to previous state
}

}(XRegExp));

1 change: 1 addition & 0 deletions tools/concatenate-source-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ source_files="
../src/addons/unicode/unicode-blocks.js
../src/addons/unicode/unicode-properties.js
../src/addons/matchrecursive.js
../src/addons/build.js
"

# Filename of concatenated package
Expand Down
Loading

0 comments on commit 47b338d

Please sign in to comment.