sregex
simple string regular expression that exposes matches in defined variables
install
node
$ npm install sregex
component
$ component install jwerle/sregex
bower
bower install sregex
usage
using sregex
is as simple as passing it a string
var regex = regex; // trueregex; // false
the returned regex
object from sregex
is an instance of RegExp
with a parse()
function attached to it
regex; // {}
you can easily define variables within your string sregex
string and access them from a string with the parse
function
var regex = ;var values = regex; console; // joeconsole; // 22
the str
argument for sregex
accepts a string, but it also can have valid regular expression interpolated
var regex = ;var values = regex; console; // foogotsconsole; // 45console; // apple
it can also accept an instance of `RegExp as input
var regex = ;var values = regex; console; // noob
building a regular expression router
building a router that parses url parameters can be simple as well
var http = sregex = http;
api
sregex(str)
converts a string to regular expression and allows retrieval of defined variables when parsing
str
- a string to convert to regular expression
example
var regex =
.parse(str)
parses a given string and returns an object representing the values extracted using the regular expression used to create it
str
- a string to parse and extact values from based on regular expression matches
example
var regex = var values = regex; console; // edit
example
we can convert a object
to a JSON
string and then to binary and attach it to the string
var regex = var bytes = ; var data = id: 1234 date: Date; var str = JSON for var i = 0; i < strlength; ++i bytes; var values = regex; console; // '123,34,105,100,34,58,49,50,51,52,44,34,100,97,116,101,34,58,49,51,55,49,53,56,51,52,51,53,52,52,48,125' // parse it back into a JSON string var parsed = ''valuesdata; console; // {"id":1234,"date":1371583641484}console; // { id: 1234, date: 1371583754259 }
license
MIT