Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RegExp.prototype.generator = function(string, submatch=null) {
- var regexp = new RegExp(this.source, 'g')
- return function *f() {
- var match = null
- while(match = regexp.exec(string)) {
- if(submatch === null)
- yield match
- else if(match.length <= submatch )
- throw('number of submatches does not match param submatch')
- else
- yield match[submatch]
- }
- return
- }()
- }
- RegExp.prototype.execArray = function(string, submatch) {
- return [...this.generator(string, submatch)]
- }
- String.prototype.matchArray = function(regExpAsString, submatch) {
- return (new RegExp(regExpAsString)).execArray(this, submatch)
- }
- var foo = "'str1', 'str2', 'str3'"
- var r = new RegExp(/\'([^'])+\'/)
- foo.matchArray(r, 1)
- foo.matchArray(/\'([^'])+\'/, 1)
- r.execArray(foo, 1)
- >>> ["str1", "str2", "str3"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement