Skip to content
Browse files

update comments

  • Loading branch information...
1 parent 0e72031 commit c4b53d4f68ddf8b36c8f0fb0c408375219009858 @slevithan committed Apr 21, 2012
Showing with 36 additions and 35 deletions.
  1. +8 −9 README.md
  2. +7 −6 src/addons/build.js
  3. +7 −7 src/xregexp.js
  4. +14 −13 xregexp-all.js
View
17 README.md
@@ -171,16 +171,17 @@ First include the script:
You can then build regular expressions using 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('(?n)(rgb|hsl)a?\\((\\s*{{number}}%?\\s*,?\\s*){3,4}\\)', {
- number: /-?\d+(?:\.\d+)?/
+XRegExp.build('(?i)\\b{{month}}{{separator}}{{year}}\\b', {
+ month: XRegExp.build('{{monthAbbr}}|{{monthName}}', {
+ monthAbbr: /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/,
+ monthName: /January|February|March|April|May|June|July|August|September|October|November|December/
}),
- hex: /#(?:[0-9A-Fa-f]{1,2}){3}/
+ separator: /,? /,
+ year: /\d{4}/
});
~~~
-The `{{…}}` syntax works only for regexes created by `XRegExp.build`. It can be escaped using `\{{…}}`. Named subpatterns can be provided as strings or regex objects. Their values are automatically wrapped in `(?:…)` so they don't interfere with the surrounding pattern in unexpected ways. If present, a leading `^` and trailing unescaped `$` are stripped from subpatterns provided as regex objects. Flags can be provided via `XRegExp.build`'s optional third argument. Backreferences cannot be used within `XRegExp.build` patterns (an error is thrown).
+The `{{…}}` syntax works only for regexes created by `XRegExp.build`. It can be escaped using `\{{…}}`. Named subpatterns can be provided as strings or regex objects. Their values are automatically wrapped in `(?:…)` so they can be quantified as a single unit and don't interfere with the surrounding pattern in unexpected ways. If present, a leading `^` and trailing unescaped `$` are stripped from subpatterns provided as regex objects. Flags can be provided via `XRegExp.build`'s optional third argument. Backreferences are not allowed within `XRegExp.build` patterns.
See also: *[Creating Grammatical Regexes Using XRegExp.build](http://blog.stevenlevithan.com/archives/grammatical-patterns-xregexp-build)*.
@@ -243,9 +244,7 @@ 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).
-`XRegExp.build` inspired by [Lea Verou](http://lea.verou.me/)'s [RegExp.create](http://lea.verou.me/2011/03/create-complex-regexps-more-easily/).
-
-`XRegExp.union` inspired by [Ruby](http://www.ruby-lang.org/).
+Prior art: `XRegExp.build` inspired by [Lea Verou](http://lea.verou.me/)'s [RegExp.create](http://lea.verou.me/2011/03/create-complex-regexps-more-easily/). `XRegExp.union` inspired by [Ruby](http://www.ruby-lang.org/). XRegExp's syntax extensions come from Perl, .NET, etc.
All code released under the [MIT License](http://mit-license.org/).
View
13 src/addons/build.js
@@ -1,5 +1,5 @@
/*!
- * XRegExp.build v0.1.0-rc-2, 2012-04-19
+ * XRegExp.build v0.1.0-rc-2, 2012-04-21
* (c) 2012 Steven Levithan <http://xregexp.com/>
* MIT License
* Based on RegExp.create by Lea Verou <http://lea.verou.me/>
@@ -49,12 +49,13 @@
* @returns {RegExp} Extended regular expression object.
* @example
*
- * var color = XRegExp.build('{{keyword}}|{{func}}|{{hex}}', {
- * keyword: /red|tan|[a-z]{4,20}/,
- * func: XRegExp.build('(?n)(rgb|hsl)a?\\((\\s*{{number}}%?\\s*,?\\s*){3,4}\\)', {
- * number: /-?\d+(?:\.\d+)?/
+ * XRegExp.build('(?i)\\b{{month}}{{separator}}{{year}}\\b', {
+ * month: XRegExp.build('{{monthAbbr}}|{{monthName}}', {
+ * monthAbbr: /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/,
+ * monthName: /January|February|March|April|May|June|July|August|September|October|November|December/
* }),
- * hex: /#(?:[0-9A-Fa-f]{1,2}){3}/
+ * separator: /,? /,
+ * year: /\d{4}/
* });
*/
XRegExp.build = function (pattern, subs, flags) {
View
14 src/xregexp.js
@@ -1,5 +1,5 @@
/*!
- * XRegExp v2.0.0-rc-2, 2012-04-20
+ * XRegExp v2.0.0-rc-2, 2012-04-21
* (c) 2007-2012 Steven Levithan <http://xregexp.com/>
* MIT License
*/
@@ -665,12 +665,12 @@ XRegExp = XRegExp || (function (undef) {
* @param {String} str String to search.
* @param {RegExp|String} search Search pattern to be replaced.
* @param {String|Function} replacement Replacement string or a function invoked to create it.
- * Replacement strings can include special replacement patterns:
- * <li>$$ - Inserts a "$".
- * <li>$& - Inserts the matched substring.
- * <li>$` - Inserts the string portion that precedes the matched substring.
- * <li>$' - Inserts the string portion that follows the matched substring.
- * <li>$n/$nn - Where n/nn are digits referencing an existent capturing group, inserts
+ * Replacement strings can include special replacement syntax:
+ * <li>$$ - Inserts a literal '$'.
+ * <li>$&, $0 - Inserts the matched substring.
+ * <li>$` - Inserts the string that precedes the matched substring (left context).
+ * <li>$' - Inserts the string that follows the matched substring (right context).
+ * <li>$n, $nn - Where n/nn are digits referencing an existent capturing group, inserts
* backreference n/nn.
* <li>${n} - Where n is a name or any number of digits that reference an existent capturing
* group, inserts backreference n.
View
27 xregexp-all.js
@@ -2,7 +2,7 @@
/***** xregexp.js *****/
/*!
- * XRegExp v2.0.0-rc-2, 2012-04-20
+ * XRegExp v2.0.0-rc-2, 2012-04-21
* (c) 2007-2012 Steven Levithan <http://xregexp.com/>
* MIT License
*/
@@ -668,12 +668,12 @@ XRegExp = XRegExp || (function (undef) {
* @param {String} str String to search.
* @param {RegExp|String} search Search pattern to be replaced.
* @param {String|Function} replacement Replacement string or a function invoked to create it.
- * Replacement strings can include special replacement patterns:
- * <li>$$ - Inserts a "$".
- * <li>$& - Inserts the matched substring.
- * <li>$` - Inserts the string portion that precedes the matched substring.
- * <li>$' - Inserts the string portion that follows the matched substring.
- * <li>$n/$nn - Where n/nn are digits referencing an existent capturing group, inserts
+ * Replacement strings can include special replacement syntax:
+ * <li>$$ - Inserts a literal '$'.
+ * <li>$&, $0 - Inserts the matched substring.
+ * <li>$` - Inserts the string that precedes the matched substring (left context).
+ * <li>$' - Inserts the string that follows the matched substring (right context).
+ * <li>$n, $nn - Where n/nn are digits referencing an existent capturing group, inserts
* backreference n/nn.
* <li>${n} - Where n is a name or any number of digits that reference an existent capturing
* group, inserts backreference n.
@@ -2065,7 +2065,7 @@ XRegExp = XRegExp || (function (undef) {
/***** build.js *****/
/*!
- * XRegExp.build v0.1.0-rc-2, 2012-04-19
+ * XRegExp.build v0.1.0-rc-2, 2012-04-21
* (c) 2012 Steven Levithan <http://xregexp.com/>
* MIT License
* Based on RegExp.create by Lea Verou <http://lea.verou.me/>
@@ -2115,12 +2115,13 @@ XRegExp = XRegExp || (function (undef) {
* @returns {RegExp} Extended regular expression object.
* @example
*
- * var color = XRegExp.build('{{keyword}}|{{func}}|{{hex}}', {
- * keyword: /red|tan|[a-z]{4,20}/,
- * func: XRegExp.build('(?n)(rgb|hsl)a?\\((\\s*{{number}}%?\\s*,?\\s*){3,4}\\)', {
- * number: /-?\d+(?:\.\d+)?/
+ * XRegExp.build('(?i)\\b{{month}}{{separator}}{{year}}\\b', {
+ * month: XRegExp.build('{{monthAbbr}}|{{monthName}}', {
+ * monthAbbr: /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/,
+ * monthName: /January|February|March|April|May|June|July|August|September|October|November|December/
* }),
- * hex: /#(?:[0-9A-Fa-f]{1,2}){3}/
+ * separator: /,? /,
+ * year: /\d{4}/
* });
*/
XRegExp.build = function (pattern, subs, flags) {

0 comments on commit c4b53d4

Please sign in to comment.
Something went wrong with that request. Please try again.