if (str.slice(-s1.length) == s1) { }
Or, less dynamic and more literal:
if (str.slice(-7) == s1) { }
Using a negative offset for slice () sets the starting point from the end of the line, minus a negative start - in this case, 7 characters (or s1.length) from the end.
slice () - MDC
Adding this to the prototype string is easy:
String.prototype.endsWith = function (str) { return this.slice(-str.length) === str; } alert("w,w,a,$,b,c".endsWith(s1));
source share