Javascript String between(prefix, suffix)

Description

Javascript String between(prefix, suffix)


/**/*from  ww w. java  2s .c  om*/
 * Usage  var you = 'hello you guys'.between('hello ',' guys');
 * you = 'you';
 */
String.prototype.between = function(prefix, suffix) {
  s = this;
  var i = s.indexOf(prefix);
  if (i >= 0) {
    s = s.substring(i + prefix.length);
  }
  else {
    return '';
  }
  if (suffix) {
    i = s.indexOf(suffix);
    if (i >= 0) {
      s = s.substring(0, i);
    }
    else {
      return '';
    }
  }
  return s;
}

Javascript String between(prefix, suffix)

String.prototype.between = function(prefix, suffix) {
   s = this;//  w w  w  .jav  a 2s.  co  m
     var i = s.indexOf(prefix);
    if (i >= 0) {
         s = s.substring(i + prefix.length);
        }
      else {
        return '';
       }
     if (suffix) {
          i = s.indexOf(suffix);
           if (i >= 0) {
               s = s.substring(0, i);
             }
            else {
                return '';
              }
           }
       return s;
}



PreviousNext

Related