Nodejs String Scan scan(regex)

Here you can find the source of scan(regex)

Method Source Code

String.prototype.scan = function (regex) {
  if (!regex.global) throw "Scan Error";
  var self = this;
  var match, occurrences = [];
  while (match = regex.exec(self)) {
    match.shift();//from   ww  w  .  j  a va  2  s . c  o  m
    occurrences.push(match[0]);
  }
  return occurrences;
};

String.prototype.splice = function(index, remove, string) {
  return this.slice(0, index) + string + this.slice(index + Math.abs(remove));
};

Array.prototype.firstElement = function() {
  return this[0];
}

Array.prototype.lastElement = function() {
  return this[this.length - 1];
}

Array.prototype.lastIndex = function() {
  return this.length - 1;
}

Array.prototype.remove = function(element) {
  var index = this.indexOf(element);
  if(index != -1) {
    this.splice(index, 1);
  }
}

Related

  1. scan(pattern)
    String.prototype.scan = function(pattern){
      if (Object.isString(pattern))
        pattern = RegExp.escape(pattern);
      return this.match(pattern);
    };
    
  2. scan(pattern, iterator)
    String.prototype.scan = function(pattern, iterator) {
      this.gsub(pattern, iterator);
      return String(this);
    };
    
  3. scan(re)
    String.prototype.scan = function (re) {
        if (!re.global) throw "RegExp should be global";
        var s = this;
        var m, r = [];
        while (m = re.exec(s)) {
            m.shift();
            r.push(m);
        return r;
    ...