Skip to content

kasium/RegExpBuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RegExpBuilder

This little javascript libary helps you to write more readable regex code in javascript.

Installation

Just add the RegExpBuilder.js file from the js folder to your project.

##Compatibility This libary is compatible with everything which has ECMAScript5 implemented. The tests are using the promise feature of ECMAScript6.

##Examples

Floating Point Numbers

var builder = new RegExpBuilder();
builder.startLine().matchesFor("-+").zeroOrOneTimes()
 .and().matchesDigit().zeroOrMoreTimes()
 .and().matchesText(".").zeroOrOneTimes()
 .and().matchesDigit().oneOrMoreTimes()
 .beginGroup()
	.matchesFor("eE")
	.and().matchesFor("-+").zeroOrOneTimes()
	.matchesDigit().oneOrMoreTimes()
 .endGroup().oneOrMoreTimes().endLine();
 var regExp = builder.build();

The and() method is only optional so theoretical it could be omitting.

This is equivalent to:

var regExp = new RegExp("^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$");

Or without using a string:

var regExp = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;

Name selecttion

Get the first last name with a beginning 'A'.

Text: Lewis,Amelia;Winston,Oliver;Adams,Emily;Ashton,Thomas

var regExp = new RegExp("A[a-zA-Z]*,[a-zA-Z]+");

Or with the builder:

var builder = new RegExpBuilder();
builder.matchesText("A").matchesFor("a-zA-Z").zeroOrMoreTimes().matchesText(",")
  matchesFor("a-zA-Z").oneOrMoreTimes();
var regExp = builder.build();

Tests

The tests are written for qUnit.

License

Refer for the license the license file