A string parser for node.js. Foucs on string operating, and more simplely regular expression writting.
Installation
npm install --save str-parser
Quickly Start
var _s = ; var str1 = '<a href="http://lellansin.com"></a>';console;// [ 'href="http://lellansin.com"', 'http://lellansin.com' ] var str2 = '<a></a>';console;// [ ] var str3 = 'Alan Bob Cici';console;// ['Alan', 'Bob', 'Cici']
Doc
.get
- directly match.Cursor
- get index for slice.Slice
- easily slice by cursor..map
- map for regex results..reduce
- reduce for regex results..filter
- filter for regex results..split
- more powerful split method.
get( expr [, n] )
Directly get the regex match part.
Arguments
expr
- RegExp object.n
- directly return $n captue value string.
Example
var _s = ; var str1 = '<a href="http://lellansin.com"></a>';console;// 'http://lellansin.com'
If the api end with 's', it means the result is still wraped by str-parser.
var _s = ; var str1 = '<a href="http://lellansin.com"></a>';console;// 'lellansin.co'
Cursor
- find
- indexOf
- rfind
- lastIndexOf
Cursor method will return str-parser object defaultly.
Slice
Slice with cursor.
var _s = ; console;// world console;// worl console;// hello console;// ello w
map( expr[, iteratee ] )
Arguments
expr
- RegExp object.iteratee(match, [ $1, $2, ..., ] offset, string)
- A function to apply to each RegExp match string.
Example
var _s = ; var html = '...' + '<a href="http://google.com">Google</a> ...' + '<a href="http://lellansin.com">My blog</a> ...' + '<a href="http://facebook.com">FaceBook</a> ...'; console;/*[ 'href="http://google.com"', 'href="http://lellansin.com"', 'href="http://facebook.com"' ]*/
$ capture
var html = '...' + '<a href="http://google.com">Google</a> ...' + '<a href="http://lellansin.com">My blog</a> ...' + '<a href="http://facebook.com">FaceBook</a> ...'; console;/*[ 'http://google.com', 'http://lellansin.com', 'http://facebook.com' ]*/
Related
- maps(expr, [iteratee])
reduce( expr[, iteratee ] )
Arguments
expr
- RegExp object.iteratee(previousValue, currentValue, currentIndex, matchList)
- A function to apply to reduce each RegExp match string.
Example
var _s = console// a,b,c,d,e
$ capture is not support for .reduce, only for .map, pls use .map + original array reduce:
var _s = var html = '...' + '<a href="http://google.com">Google</a> ...' + '<a href="http://lellansin.com">My blog</a> ...' + '<a href="http://facebook.com">FaceBook</a> ...'; console// Google, My blog, FaceBook
Related
- reduces(expr, [iteratee])
filter( expr[, iteratee ] )
Arguments
expr
- RegExp object.iteratee(match)
- A function to apply to filter each RegExp match string.
Example
var _s = var str = '12 1 5 6 18 101 22';console// [ '12', '18', '22' ]
Related
- filters(expr, [iteratee])
split( expr[, iteratee ] )
Arguments
expr
- RegExp object.iteratee(match, separator, offset)
- A function to apply to each RegExp match string. if you returnboolean
, it's just like.filter
for result list; if you returnnull
/undefined
, it means you abandon current match (won't come in your result list).
Example
var _s = var str = 'Wed May 04 2016 12:45:03 GMT+0800 (CST)';console// [ 'Wed', 'May', '04', '2016', '12', '45', '03', 'GMT', '0800', '(CST)' ] var str = 'Line one texts ...\n' + 'Line two\r' + 'Line three texts ...\r\n' + 'Line four ...\n';console/*[ 'LINE ONE TEXTS ...', 'LINE TWO', 'LINE THREE TEXTS ...', 'LINE FOUR ...' ]*/ var str = '12,123,456,789';console// [ 2, 6, 10 ]
Related
- splits(expr, [iteratee])