Skip to content

Commit

Permalink
Add regex flags and newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
JonAbrams committed Jun 26, 2015
1 parent 34082cc commit ad61ab1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
22 changes: 19 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,20 @@ matched:

## API

### xeger([function])
### xeger([function], [options])

Call this to start the construction of the regex, passing in a callback function. It returns a RegExp object.

Use the rest of the functions in this section (the *rule* functions) to construct a regex by calling them within the callback.

The callback function will be called with one parameter, the xeger object. The rest of the functions here should be called on the xeger object. The callback is also called with the xeger object assigned to `this`.

The options object passed here is different from the options object used in the rest of the API. This one takes the following keys:

- **global**: [boolean] - Will attempt to match the regex multiple times.
- **multiline**: [boolean] - Will attempt to match the regex multiple times.
- **insensitive**: [boolean] - Case insensitive matching.

### x.literal([string], [options])

Matches the exact string passed in. `x.literal` will escape any non-alpha numeric character.
Expand Down Expand Up @@ -122,13 +128,13 @@ xeger(function (x) {

### x.to()

Used to create the '-' inside *any* and *not* functions.
Used to create the '-' inside *any* and *not* functions (see examples for *any* and *not*).

If you were to just do `x.any('A-Z')` the `-` would be escaped: `/[A\-Z]/`

### x.alphanumeric([options])

Matches any single alpha-numeric character (i.e. letters and numbers).
Matches any single alpha-numeric character (includes letters, numbers, and the underscore).

```javascript
xeger(function (x) {
Expand Down Expand Up @@ -156,6 +162,16 @@ xeger(function (x) {
}); /* returns /\s/ */
```

### x.newline([options])

Matches a newline character

```javascript
xeger(function (x) {
x.newline();
}); /* returns /\n/ */
```

### x.start()

Matches the start of the string.
Expand Down
17 changes: 16 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ describe('xeger', function () {
r.literal('hello');
r.alphanumeric();
r.number();
r.newline();
r.end();
});
assert.equal(regex.toString(), '/^hello\\w\\d$/');
assert.equal(regex.toString(), '/^hello\\w\\d\\n$/');
});

describe('flags', function () {
var regex = xeger(function (r) {
r.literal('hi');
}, {
global: true,
multiline: true,
insensitive: true
});

it('adds flags', function () {
assert.equal(regex.toString(), '/hi/gim');
});
});

describe('some options', function () {
Expand Down
25 changes: 21 additions & 4 deletions xeger.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
module.exports = function (cb) {
var r = new Xeger(cb);
module.exports = function (cb, options) {
var r = new Xeger(cb, options);

return r.regex();
};


var Xeger = function (cb) {
var Xeger = function (cb, options) {
options = options || {};
this.regexStr = '';
this.flags = '';

if (options.multiline) {
this.flags += 'm';
}
if (options.global) {
this.flags += 'g';
}
if (options.insensitive) {
this.flags += 'i';
}
cb.call(this, this);
};

Expand Down Expand Up @@ -37,6 +49,11 @@ Xeger.prototype.number = function (options) {
this.addOptions(options);
};

Xeger.prototype.newline = function (options) {
this.add('\\n');
this.addOptions(options);
};

Xeger.prototype.start = function () {
this.add('^');
};
Expand Down Expand Up @@ -89,7 +106,7 @@ Xeger.prototype.group = function (cb, options) {
/* Private */

Xeger.prototype.regex = function () {
return new RegExp(this.regexStr);
return new RegExp(this.regexStr, this.flags);
};

Xeger.prototype.addOptions = function (options) {
Expand Down

0 comments on commit ad61ab1

Please sign in to comment.