# Fragments Of Time

## RegexGen.js - JavaScript Regular Expression Generator

RegexGen.js is a JavaScript Regular Expression Generator that helps to construct complex regular expressions, inspired by JSVerbalExpressions.

### The Problems

RegexGen.js tries to ease two problems.

1. While creating a regular expression, it's hard to remember the correct syntax and what characters to escape.
2. After done creating a regular expression, it's hard to read and remember what the regex do.

### The Goals

RegexGen.js is designed to achieve the following goals.

1. The written codes should be easy to read and easy to understand.
2. The generated code should be as compact as possible, e.g., no redundant brackets and parentheses.
3. No more character escaping reguired (except '\', or if you use regex overwrite.)
4. If the generated code is not good enougth, bad parts can be easily replaced directly in the written codes.

### Getting Started

The generator is exported as a regexGen() function.

To generate a regular expression, pass sub-expressions as parameters to the regexGen() function.

Sub-expressions as parameters which are separated by comma are concatenated together to form the whole regular expression.

Sub-expressions can either be a string, a number, a RegExp object, or any values generated by the owned functions of the regexGen() function object, i.e., the regex-generator() as the following informal BNF syntax.

Strings passed into the regexGen(), the text(), the maybe(), the anyCharOf() and the anyCharBut() functions, are always escaped as necessary, so you don't have to worry about which characters to escape.

The result of calling the regexGen() function is a RegExp object.

The basic usage can be expressed as the following informal BNF syntax.

Please check out regexgen.js and wiki for API documentations, and check out test.js for more examples.

### Installation

If your are managing package dependencies with bower, your can install RegexGen.js using bower install command.

### Usage

#### The hard (but safe) way

Since the generator is exported as the regexGen() function.
Everything must be referenced from it.
To simplify the code, assign it to a short variable is preferable.

#### Mixin to global object

If you still feel inconvenient, and don't mind the global object being polluted,
use the regexGen.mixin() function to export all member functions of the regexGen() function object to the global object.

#### Use the with keyword

Or, if you don't use the strict mode with use strict keyword,
you can use the with keyword to refer to all member functions of the regexGen() function object.

### Examples

This example is taken from the article: Mastering Lookahead and Lookbehind.

Generates:

This example is taken from the book: Mastering Regular Expressions

Generates:

##### Matching Balanced Sets of Parentheses

This example is taken from the book: Mastering Regular Expressions

Generates:

##### Matching Balanced Sets of Parentheses within Any Given Levels of Depth

This example is taken from the book: Mastering Regular Expressions

Given 1 level of nesting:

Generates:

Given 3 levels of nesting:

Generates:

##### Matching an HTML Tag

This example is taken from the book: Mastering Regular Expressions

Generates:

This example is taken from the book: Mastering Regular Expressions

Generates:

Here's how to iterate all links:

##### Examining an HTTP URL

This example is taken from the book: Mastering Regular Expressions

Generates:

Here's a snippet to report about a URL:

##### Validating a Hostname

This example is taken from the book: Mastering Regular Expressions

Generates:

##### Parsing CSV Files

This example is taken from the book: Mastering Regular Expressions

Generates: