Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
9.7 KiB

  1. # XRegExp 4.0.0
  2. [![Build Status](https://travis-ci.org/slevithan/xregexp.svg?branch=master)](https://travis-ci.org/slevithan/xregexp)
  3. XRegExp provides augmented (and extensible) JavaScript regular expressions. You get modern syntax and flags beyond what browsers support natively. XRegExp is also a regex utility belt with tools to make your grepping and parsing easier, while freeing you from regex cross-browser inconsistencies and other annoyances.
  4. XRegExp supports all native ES6 regular expression syntax. It supports ES5+ browsers, and you can use it with Node.js or as a RequireJS module.
  5. ## Performance
  6. XRegExp compiles to native `RegExp` objects. Therefore regexes built with XRegExp perform just as fast as native regular expressions. There is a tiny extra cost when compiling a pattern for the first time.
  7. ## Usage examples
  8. ```js
  9. // Using named capture and flag x for free-spacing and line comments
  10. const date = XRegExp(
  11. `(?<year> [0-9]{4} ) -? # year
  12. (?<month> [0-9]{2} ) -? # month
  13. (?<day> [0-9]{2} ) # day`, 'x');
  14. // XRegExp.exec gives you named backreferences on the match result
  15. let match = XRegExp.exec('2017-02-22', date);
  16. match.year; // -> '2017'
  17. // It also includes optional pos and sticky arguments
  18. let pos = 3;
  19. const result = [];
  20. while (match = XRegExp.exec('<1><2><3>4<5>', /<(\d+)>/, pos, 'sticky')) {
  21. result.push(match[1]);
  22. pos = match.index + match[0].length;
  23. }
  24. // result -> ['2', '3']
  25. // XRegExp.replace allows named backreferences in replacements
  26. XRegExp.replace('2017-02-22', date, '$<month>/$<day>/$<year>');
  27. // -> '02/22/2017'
  28. XRegExp.replace('2017-02-22', date, (match) => {
  29. return `${match.month}/${match.day}/${match.year}`;
  30. });
  31. // -> '02/22/2017'
  32. // XRegExps compile to RegExps and work perfectly with native methods
  33. date.test('2017-02-22');
  34. // -> true
  35. // The only caveat is that named captures must be referenced using
  36. // numbered backreferences if used with native methods
  37. '2017-02-22'.replace(date, '$2/$3/$1');
  38. // -> '02/22/2017'
  39. // Use XRegExp.forEach to extract every other digit from a string
  40. const evens = [];
  41. XRegExp.forEach('1a2345', /\d/, (match, i) => {
  42. if (i % 2) evens.push(+match[0]);
  43. });
  44. // evens -> [2, 4]
  45. // Use XRegExp.matchChain to get numbers within <b> tags
  46. XRegExp.matchChain('1 <b>2</b> 3 <B>4 \n 56</B>', [
  47. XRegExp('(?is)<b>.*?</b>'),
  48. /\d+/
  49. ]);
  50. // -> ['2', '4', '56']
  51. // You can also pass forward and return specific backreferences
  52. const html =
  53. `<a href="http://xregexp.com/">XRegExp</a>
  54. <a href="http://www.google.com/">Google</a>`;
  55. XRegExp.matchChain(html, [
  56. {regex: /<a href="([^"]+)">/i, backref: 1},
  57. {regex: XRegExp('(?i)^https?://(?<domain>[^/?#]+)'), backref: 'domain'}
  58. ]);
  59. // -> ['xregexp.com', 'www.google.com']
  60. // Merge strings and regexes, with updated backreferences
  61. XRegExp.union(['m+a*n', /(bear)\1/, /(pig)\1/], 'i', {conjunction: 'or'});
  62. // -> /m\+a\*n|(bear)\1|(pig)\2/i
  63. ```
  64. These examples give the flavor of what's possible, but XRegExp has more syntax, flags, methods, options, and browser fixes that aren't shown here. You can also augment XRegExp's regular expression syntax with addons (see below) or write your own. See [xregexp.com](http://xregexp.com/) for details.
  65. ## Addons
  66. You can either load addons individually, or bundle all addons with XRegExp by loading `xregexp-all.js` from https://unpkg.com/xregexp/xregexp-all.js.
  67. ### Unicode
  68. If not using `xregexp-all.js`, first include the Unicode Base script and then one or more of the addons for Unicode blocks, categories, properties, or scripts.
  69. Then you can do this:
  70. ```js
  71. // Test the Unicode category L (Letter)
  72. const unicodeWord = XRegExp('^\\pL+$');
  73. unicodeWord.test('Русский'); // -> true
  74. unicodeWord.test('日本語'); // -> true
  75. unicodeWord.test('العربية'); // -> true
  76. // Test some Unicode scripts
  77. XRegExp('^\\p{Hiragana}+$').test('ひらがな'); // -> true
  78. XRegExp('^[\\p{Latin}\\p{Common}]+$').test('Über Café.'); // -> true
  79. ```
  80. By default, `\p{…}` and `\P{…}` support the Basic Multilingual Plane (i.e. code points up to `U+FFFF`). You can opt-in to full 21-bit Unicode support (with code points up to `U+10FFFF`) on a per-regex basis by using flag `A`. This is called *astral mode*. You can automatically add flag `A` for all new regexes by running `XRegExp.install('astral')`. When in astral mode, `\p{…}` and `\P{…}` always match a full code point rather than a code unit, using surrogate pairs for code points above `U+FFFF`.
  81. ```js
  82. // Using flag A to match astral code points
  83. XRegExp('^\\pS$').test('💩'); // -> false
  84. XRegExp('^\\pS$', 'A').test('💩'); // -> true
  85. XRegExp('(?A)^\\pS$').test('💩'); // -> true
  86. // Using surrogate pair U+D83D U+DCA9 to represent U+1F4A9 (pile of poo)
  87. XRegExp('(?A)^\\pS$').test('\uD83D\uDCA9'); // -> true
  88. // Implicit flag A
  89. XRegExp.install('astral');
  90. XRegExp('^\\pS$').test('💩'); // -> true
  91. ```
  92. Opting in to astral mode disables the use of `\p{…}` and `\P{…}` within character classes. In astral mode, use e.g. `(\pL|[0-9_])+` instead of `[\pL0-9_]+`.
  93. XRegExp uses Unicode 9.0.0.
  94. ### XRegExp.build
  95. Build regular expressions using named subpatterns, for readability and pattern reuse:
  96. ```js
  97. const time = XRegExp.build('(?x)^ {{hours}} ({{minutes}}) $', {
  98. hours: XRegExp.build('{{h12}} : | {{h24}}', {
  99. h12: /1[0-2]|0?[1-9]/,
  100. h24: /2[0-3]|[01][0-9]/
  101. }),
  102. minutes: /^[0-5][0-9]$/
  103. });
  104. time.test('10:59'); // -> true
  105. XRegExp.exec('10:59', time).minutes; // -> '59'
  106. ```
  107. 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. Any backreferences in the outer pattern or 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}})`. Named subpatterns cannot be embedded within character classes.
  108. #### XRegExp.tag (included with XRegExp.build)
  109. Provides tagged template literals that create regexes with XRegExp syntax and flags:
  110. ```js
  111. const h12 = /1[0-2]|0?[1-9]/;
  112. const h24 = /2[0-3]|[01][0-9]/;
  113. const hours = XRegExp.tag('x')`${h12} : | ${h24}`;
  114. const minutes = /^[0-5][0-9]$/;
  115. // Note that explicitly naming the 'minutes' group is required for named backreferences
  116. const time = XRegExp.tag('x')`^ ${hours} (?<minutes>${minutes}) $`;
  117. time.test('10:59'); // -> true
  118. XRegExp.exec('10:59', time).minutes; // -> '59'
  119. ```
  120. XRegExp.tag does more than just basic interpolation. For starters, you get all the XRegExp syntax and flags. Even better, since `XRegExp.tag` uses your pattern as a raw string, you no longer need to escape all your backslashes. And since it relies on `XRegExp.build` under the hood, you get all of its extras for free. Leading `^` and trailing unescaped `$` are stripped from interpolated patterns if both are present (to allow embedding independently useful anchored regexes), interpolating into a character class is an error (to avoid unintended meaning in edge cases), interpolated patterns are treated as atomic units when quantified, interpolated strings have their special characters escaped, and any backreferences within an interpolated regex are rewritten to work within the overall pattern.
  121. ### XRegExp.matchRecursive
  122. Match recursive constructs using XRegExp pattern strings as left and right delimiters:
  123. ```js
  124. const str1 = '(t((e))s)t()(ing)';
  125. XRegExp.matchRecursive(str1, '\\(', '\\)', 'g');
  126. // -> ['t((e))s', '', 'ing']
  127. // Extended information mode with valueNames
  128. const str2 = 'Here is <div> <div>an</div></div> example';
  129. XRegExp.matchRecursive(str2, '<div\\s*>', '</div>', 'gi', {
  130. valueNames: ['between', 'left', 'match', 'right']
  131. });
  132. /* -> [
  133. {name: 'between', value: 'Here is ', start: 0, end: 8},
  134. {name: 'left', value: '<div>', start: 8, end: 13},
  135. {name: 'match', value: ' <div>an</div>', start: 13, end: 27},
  136. {name: 'right', value: '</div>', start: 27, end: 33},
  137. {name: 'between', value: ' example', start: 33, end: 41}
  138. ] */
  139. // Omitting unneeded parts with null valueNames, and using escapeChar
  140. const str3 = '...{1}.\\{{function(x,y){return {y:x}}}';
  141. XRegExp.matchRecursive(str3, '{', '}', 'g', {
  142. valueNames: ['literal', null, 'value', null],
  143. escapeChar: '\\'
  144. });
  145. /* -> [
  146. {name: 'literal', value: '...', start: 0, end: 3},
  147. {name: 'value', value: '1', start: 4, end: 5},
  148. {name: 'literal', value: '.\\{', start: 6, end: 9},
  149. {name: 'value', value: 'function(x,y){return {y:x}}', start: 10, end: 37}
  150. ] */
  151. // Sticky mode via flag y
  152. const str4 = '<1><<<2>>><3>4<5>';
  153. XRegExp.matchRecursive(str4, '<', '>', 'gy');
  154. // -> ['1', '<<2>>', '3']
  155. ```
  156. `XRegExp.matchRecursive` throws an error if it scans past an unbalanced delimiter in the target string.
  157. ## Installation and usage
  158. In browsers (bundle XRegExp with all of its addons):
  159. ```html
  160. <script src="https://unpkg.com/xregexp/xregexp-all.js"></script>
  161. ```
  162. Using [npm](https://www.npmjs.com/):
  163. ```bash
  164. npm install xregexp
  165. ```
  166. In [Node.js](http://nodejs.org/):
  167. ```js
  168. const XRegExp = require('xregexp');
  169. ```
  170. In an AMD loader like [RequireJS](http://requirejs.org/):
  171. ```js
  172. require({paths: {xregexp: 'xregexp-all'}}, ['xregexp'], (XRegExp) => {
  173. console.log(XRegExp.version);
  174. });
  175. ```
  176. ## About
  177. XRegExp copyright 2007-2017 by [Steven Levithan](http://stevenlevithan.com/). Unicode data generators by [Mathias Bynens](http://mathiasbynens.be/), adapted from [unicode-data](http://git.io/unicode). XRegExp's syntax extensions and flags come from [Perl](http://www.perl.org/), [.NET](http://www.microsoft.com/net), etc.
  178. All code, including addons, tools, and tests, is released under the terms of the [MIT License](http://mit-license.org/).
  179. Learn more at [xregexp.com](http://xregexp.com/).