restream
Regular Expression Transform Stream for Node.js
restream(regex: RegExp) => Transform
Create a transform stream which will buffer incoming data and push regex results
when matches can be made, i.e. when regex.exec
returns non-null value. You will probably want to
set the g
flag on regexes most of the time.
const restream = require('restream')
const Readable = require('stream').Readable
const Writable = require('stream').Writable
const input = 'test-string-{12345}-{67890}'
const rs = new Readable({
read: () => {
rs.push(input)
rs.push(null)
},
})
const ws = new Writable({ objectMode: true })
const data = []
ws._write = (chunk, _, next) => {
data.push(chunk)
next()
}
const regex = /{(\d+)}/g
const rts = restream(regex)
rs.pipe(rts).pipe(ws)
ws.once('finish', () => {
console.log(data)
})
[ [ '{12345}',
'12345',
index: 12,
input: 'test-string-{12345}-{67890}' ],
[ '{67890}',
'67890',
index: 20,
input: 'test-string-{12345}-{67890}' ] ]
Copyright
2017, Sobesednik Media