Extends the String object, creating a "endsWith" method on it. - Node.js String

Node.js examples for String:String End

Description

Extends the String object, creating a "endsWith" method on it.

Demo Code

String.prototype.endsWith = function( value, ignoreCase )
{
  var L1 = this.length ;
  var L2 = value.length ;
  
  if ( L2 > L1 )
    return false ;

  if ( ignoreCase )
  {//from ww  w  .j  a v  a2 s  .c o  m
    var oRegex = new RegExp( value + '$' , 'i' ) ;
    return oRegex.test( this ) ;
  }
  else
    return ( L2 == 0 || this.substr( L1 - L2, L2 ) == value ) ;
}

Related Tutorials