https://github.com/klippersubs/xre
XRegExp regular expressions without the pain in the ass.
https://github.com/klippersubs/xre
regex regexp tagged-template-literals template-literals xregexp
Last synced: 9 days ago
JSON representation
XRegExp regular expressions without the pain in the ass.
- Host: GitHub
- URL: https://github.com/klippersubs/xre
- Owner: klippersubs
- Created: 2017-06-11T02:13:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-08T05:39:41.000Z (about 8 years ago)
- Last Synced: 2025-09-08T23:44:04.613Z (about 1 month ago)
- Topics: regex, regexp, tagged-template-literals, template-literals, xregexp
- Language: JavaScript
- Size: 36.1 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# xre
[][ci]
[][npm]
[][npm]
[][npm]> [XRegExp][xregexp] regular expressions without the pain in the ass.
````bash
$ yarn add xre
````## Examples
### Multi-line RegExp with comments
````javascript
import xre from 'xre';const date = xre`/
(? \d{4}) - # Year
(? \d{2}) - # Month
(? \d{2}) # Day
/x`;const { year, month, day } = date.exec('2017-06-11');
console.log(`Year: ${year}, month: ${month}, day: ${day}`);
// → Year: 2017, month: 06, day: 11
````### Unicode support
````javascript
import xre, { configure } from 'xre';
import XRegExp from 'xregexp';// By default xre uses minimal version of XRegExp,
// so to use the power of add-ons we have to configure it
// with full version of XRegExp:
configure({ XRegExp });const fullName = xre`/(?\p{Letter}+)\s(?\p{Letter}+)/i`;
console.log(fullName.exec('Shit Ass').firstName);
// → Shitconsole.log(fullName.exec('Говно Жопа').firstName);
// → Говноconsole.log(fullName.exec('くそ けつ').firstName);
// → くそ
````## API
All built-in `RegExp`'s [methods and properties][regexp] is fully supported,
so you can use `xre` regular expressions as an argument
for `String`'s methods such as `replace` or `match`.### Methods with non-standard behavior
#### `exec`
Arguments:
* `string: string` — string to search.
* `position: number = lastIndex` — zero-based index at which to start the search.
* `sticky: boolean | string = false` — whether the match must start at the specified position only.
The string `'sticky'` is accepted as an alternative to `true`.Returns:
* Match array with named backreference properties, or `null`.
See also:
* [XRegExp docs for `exec()` method][exec].
#### `test`
Arguments:
* See [`Xre#exec`](#exec) method arguments.
See also:
* [XRegExp docs for `test()` method][test].
### Non-standard methods
Non-standard methods are derived from XRegExp's ones, but does not take regexp argument.
* `addToken` (see [XRegExp docs for `addToken()` method][addToken]).
* `forEach` (see [XRegExp docs for `forEach()` method][forEach]).
* `globalize` (see [XRegExp docs for `globalize()` method][globalize]).
* `match` (see [XRegExp docs for `match()` method][match]).
* `replace` (see [XRegExp docs for `replace()` method][replace]).
* `split` (see [XRegExp docs for `split()` method][split]).### Additional API
#### `configure`
Arguments:
* `options`:
* `XRegExp` — XRegExp constructor.## Contributing
````bash
$ yarn run lint
$ yarn run test
````[ci]: https://travis-ci.org/aliceklipper/xre
[npm]: https://www.npmjs.com/package/xre[regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
[xregexp]: http://xregexp.com/
[exec]: http://xregexp.com/api/#exec
[test]: http://xregexp.com/api/#test
[addToken]: http://xregexp.com/api/#addToken
[forEach]: http://xregexp.com/api/#forEach
[globalize]: http://xregexp.com/api/#globalize
[match]: http://xregexp.com/api/#match
[replace]: http://xregexp.com/api/#replace
[split]: http://xregexp.com/api/#split