Thursday, October 25, 2007

Javascript: EndsWith e StartsWith

. Thursday, October 25, 2007

Attraverso i prototype è possibile espandere le funzionalità degli oggetti base.
In questo esempio implemento due funzioni che verificano se una determinata stringa termina o inizia in un certo modo:

String.prototype.endsWith = function( eString)
{
var reg = new RegExp( eString + "$");
return reg.test( this);
}


String.prototype.startsWith = function( eString)
{
var reg = new RegExp( "^" + eString);
return reg.test( this);
}

0 commenti:

Post a Comment