Skip to content
This repository
Newer
Older
100644 176 lines (130 sloc) 6.513 kb
80d1243b »
2012-03-06 bumped to v2.0.0-dev, moved legacy to backcompat.js
1 [XRegExp](http://xregexp.com/) (v2.0.0-dev)
9ef01184 »
2012-03-03 copyediting
2 =============================================
a6acc474 »
2012-02-24 first commit
3
845273d8 »
2012-03-04 readme: add tools/ author credit
4 XRegExp provides augmented, extensible JavaScript regular expressions. You get new syntax, flags, and methods beyond what browsers support natively. XRegExp is also a regular expression utility belt with tools to make your client-side grepping simpler and more powerful, while freeing you from worrying about pesky cross-browser inconsistencies and the dubious `lastIndex` property.
a6acc474 »
2012-02-24 first commit
5
5ab8962c »
2012-03-03 comments and alpha/beta version nums
6
7 ## A few examples
9e909c64 »
2012-03-02 added Unicode category aliases
8
9 ~~~ js
845273d8 »
2012-03-04 readme: add tools/ author credit
10 var date, dateStr, match, str, pos, result = [];
9e909c64 »
2012-03-02 added Unicode category aliases
11
12 // Using named capture and the x flag (free-spacing and comments)
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
13 date = XRegExp('(?<year> [0-9]{4}) -? # year \n\
14 (?<month> [0-9]{2}) -? # month \n\
15 (?<day> [0-9]{2}) # day ', 'x');
9e909c64 »
2012-03-02 added Unicode category aliases
16
17 // XRegExp.exec gives you named backreferences on the match result
18 dateStr = '2012-02-22';
19 match = XRegExp.exec(dateStr, date);
20 match.day; // -> '22'
21
22 // It also supports optional pos and sticky arguments
845273d8 »
2012-03-04 readme: add tools/ author credit
23 str = '<1><2><3><4>5<6>';
24 pos = 2;
9e909c64 »
2012-03-02 added Unicode category aliases
25 while (match = XRegExp.exec(str, XRegExp.cache('<(\\d+)>'), pos, true)) {
26 result.push(match[1]);
27 pos = match.index + match[0].length;
28 }
845273d8 »
2012-03-04 readme: add tools/ author credit
29 // result -> ['2', '3', '4']
9e909c64 »
2012-03-02 added Unicode category aliases
30
31 // XRegExp.replace allows named backreferences in replacements
32 XRegExp.replace(dateStr, date, '${month}/${day}/${year}'); // -> '02/22/2012'
33
34 // In fact, all XRegExps are RegExps and work perfectly with native methods
35 date.test(dateStr); // -> true
36
37 // The only caveat is that named captures must be referred to using numbered backreferences
38 dateStr.replace(date, '$2/$3/$1'); // -> '02/22/2012'
39
40 // If you want, you can extend native methods so you don't have to worry about this
41 XRegExp.install('natives');
42 dateStr.replace(date, '${month}/${day}/${year}'); // -> '02/22/2012'
43 dateStr.replace(date, function (match) {
44 return match.month + '/' + match.day + '/' +match.year;
45 }); // -> '02/22/2012'
46 date.exec(dateStr).day; // -> 22
47
48 // Get an array of backreference-only arrays using XRegExp.forEach
49 str = '<a href="http://xregexp.com/api/">XRegExp</a>\
50 <a href="http://www.google.com/">Google</a>';
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
51 XRegExp.forEach(str, XRegExp('<a href="([^"]+)">(.*?)</a>', 'is'), function (match) {
9e909c64 »
2012-03-02 added Unicode category aliases
52 this.push(match.slice(1));
53 }, []);
54 // -> [['http://xregexp.com/api/', 'XRegExp'], ['http://www.google.com/', 'Google']]
55
56 // Get an array of numbers within <b> tags using XRegExp.matchChain
57 XRegExp.matchChain('1 <b>2</b> 3 <b>4 a 56</b>', [/<b>.*?<\/b>/i, /\d+/]);
58 // -> ['2', '4', '56']
59
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
60 // You can also pass forward and return specific backreferences
9e909c64 »
2012-03-02 added Unicode category aliases
61 XRegExp.matchChain(str, [
62 {regex: /<a href="([^"]+)">/i, backref: 1},
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
63 {regex: XRegExp('(?i)^https?://(?<domain>[^/?#]+)'), backref: 'domain'}
9e909c64 »
2012-03-02 added Unicode category aliases
64 ]);
65 // -> ['xregexp.com', 'www.google.com']
66
67 // XRegExp regexes get call and apply methods
68 // To demonstrate, let's first create the function we'll be using...
69 function filter (array, fn) {
70 var res = [];
71 array.forEach(function (el) {if (fn.call(null, el)) res.push(el);});
72 return res;
73 }
74 // Now we can filter arrays using functions and regexes
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
75 filter(['a', 'ba', 'ab', 'b'], XRegExp('^a'));
9e909c64 »
2012-03-02 added Unicode category aliases
76 // -> ['a', 'ab']
77 ~~~
78
79 These examples should give you an idea of what's possible, but they don't show all of XRegExp's tricks. You can even augment XRegExp's regular expression syntax with addons (see below) or write your own. For the full scoop, see [API](http://xregexp.com/api/), [syntax](http://xregexp.com/syntax/), [flags](http://xregexp.com/flags/), [browser fixes](http://xregexp.com/cross_browser/), and [roadmap](https://github.com/slevithan/XRegExp/wiki/Roadmap).
80
5ab8962c »
2012-03-03 comments and alpha/beta version nums
81
362838d8 »
2012-03-05 lots o' tweaks +package.json
82 ## XRegExp Unicode Base (v1.0.0-dev)
9e909c64 »
2012-03-02 added Unicode category aliases
83
84 First include the Unicode Base script:
85
86 ~~~ html
4515d503 » mathiasbynens
2012-02-26 README: Use consistent indentation
87 <script src="xregexp.js"></script>
efee9fe0 »
2012-02-27 rename plugins to addons
88 <script src="addons/unicode/xregexp-unicode-base.js"></script>
9e909c64 »
2012-03-02 added Unicode category aliases
89 ~~~
90
91 Then you can do this:
92
93 ~~~ js
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
94 var unicodeWord = XRegExp('^\\p{L}+$');
9e909c64 »
2012-03-02 added Unicode category aliases
95 unicodeWord.test('Русский'); // -> true
96 unicodeWord.test('日本語'); // -> true
97 unicodeWord.test('العربية'); // -> true
98 ~~~
99
5ab8962c »
2012-03-03 comments and alpha/beta version nums
100 The base script adds `\p{L}` (and its alias, `\p{Letter}`), but other Unicode categories, scripts, and blocks require addon packages. Try these next examples after additionally including `xregexp-unicode-scripts.js`:
9e909c64 »
2012-03-02 added Unicode category aliases
101
102 ~~~ js
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
103 XRegExp('^\\p{Hiragana}+$').test('ひらがな'); // -> true
104 XRegExp('^[\\p{Latin}\\p{Common}]+$').test('Über Café.'); // -> true
9e909c64 »
2012-03-02 added Unicode category aliases
105 ~~~
a6acc474 »
2012-02-24 first commit
106
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
107 XRegExp uses the Unicode 6.1 character database (released January 2012).
108
109 More details: [Addons: Unicode](http://xregexp.com/plugins/#unicode).
30de2aa9 »
2012-03-01 readme update
110
5ab8962c »
2012-03-03 comments and alpha/beta version nums
111
362838d8 »
2012-03-05 lots o' tweaks +package.json
112 ## XRegExp Match Recursive (v0.2.0-dev)
30de2aa9 »
2012-03-01 readme update
113
9e909c64 »
2012-03-02 added Unicode category aliases
114 First include the Match Recursive script:
30de2aa9 »
2012-03-01 readme update
115
9e909c64 »
2012-03-02 added Unicode category aliases
116 ~~~ html
30de2aa9 »
2012-03-01 readme update
117 <script src="xregexp.js"></script>
118 <script src="addons/xregexp-matchrecursive.js"></script>
9e909c64 »
2012-03-02 added Unicode category aliases
119 ~~~
120
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
121 You can then match recursive constructs using XRegExp patterns as left and right delimiters:
9e909c64 »
2012-03-02 added Unicode category aliases
122
123 ~~~ js
124 var str = '(t((e))s)t()(ing)';
125 XRegExp.matchRecursive(str, '\\(', '\\)', 'g');
126 // -> ['t((e))s', '', 'ing']
127
128 // Extended information mode with valueNames
129 str = 'Here is <div>a <div>nested</div> tag</div> example.';
5ab8962c »
2012-03-03 comments and alpha/beta version nums
130 XRegExp.matchRecursive(str, '<div\\s*>', '</div>', 'gi', {
9e909c64 »
2012-03-02 added Unicode category aliases
131 valueNames: ['between', 'left', 'match', 'right']
132 });
133 // -> [['between', 'Here is ', 0, 8],
134 // ['left', '<div>', 8, 13],
135 // ['match', 'a <div>nested</div> tag', 13, 37],
136 // ['right', '</div>', 36, 42],
137 // ['between', ' example.', 42, 51]]
138
139 // Omitting unneeded parts with null valueNames, and using escapeChar
140 str = '...{1}\\{{function(x,y){return y+x;}}';
141 XRegExp.matchRecursive(str, '{', '}', 'g', {
142 valueNames: ['literal', null, 'value', null],
143 escapeChar: '\\'
144 });
145 // -> [['literal', '...', 0, 3],
146 // ['value', '1', 4, 5],
147 // ['literal', '\\{', 6, 8],
148 // ['value', 'function(x,y){return y+x;}', 9, 35]]
149
150 // Sticky mode via the y flag (works everywhere, not just where /y is natively supported)
151 str = '<1><<<2>>><3>4<5>';
152 XRegExp.matchRecursive(str, '<', '>', 'gy');
153 // -> ['1', '<<2>>', '3']
154 ~~~
a6acc474 »
2012-02-24 first commit
155
75aede51 »
2012-03-05 added XRegExp.install/uninstall/isInstalled
156 More details: [Addons: Match Recursive](http://xregexp.com/plugins/#matchRecursive).
5ebad916 »
2012-03-01 readme: link to match recursive api doc
157
5ab8962c »
2012-03-03 comments and alpha/beta version nums
158
9e909c64 »
2012-03-02 added Unicode category aliases
159 ## Changelog
a6acc474 »
2012-02-24 first commit
160
4f91f608 »
2012-02-27 README: update intro
161 * Historical changes: [Version history](http://xregexp.com/history/).
162 * Planned changes: [Roadmap](https://github.com/slevithan/XRegExp/wiki/Roadmap).
30de2aa9 »
2012-03-01 readme update
163
9ef01184 »
2012-03-03 copyediting
164
52b17d93 »
2012-03-06 README: About
165 ## About
9ef01184 »
2012-03-03 copyediting
166
52b17d93 »
2012-03-06 README: About
167 XRegExp and addons copyright 2007-2012 by [Steven Levithan](http://stevenlevithan.com/).
845273d8 »
2012-03-04 readme: add tools/ author credit
168
52b17d93 »
2012-03-06 README: About
169 Tools:
170
171 * Unicode range generators by [Mathias Bynens](http://mathiasbynens.be/).
172 * Source file concatenator by [Bjarke Walling](http://twitter.com/walling).
9ef01184 »
2012-03-03 copyediting
173
b028743c »
2012-03-06 +MIT License in README, url->homepage
174 All code released under the [MIT License](http://opensource.org/licenses/mit-license.php).
175
362838d8 »
2012-03-05 lots o' tweaks +package.json
176 Fork me to show support, fix, and extend.
9ef01184 »
2012-03-03 copyediting
177
Something went wrong with that request. Please try again.