How can I check if a string ends with a particular character in JavaScript?
var str = "mystring#";
String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; };
function makeSuffixRegExp(suffix, caseInsensitive) { return new RegExp( String(suffix).replace(/[$%()*+.?\[\\\]{|}]/g, "\\$&") + "$", caseInsensitive ? "i" : ""); }
and then we can use it like this
makeSuffixRegExp("a[complicated]*suffix*").test(str)
endsWith implementation:
String.prototype.endsWith = function (s) { return this.length >= s.length && this.substr(this.length - s.length) == s; }
String.prototype.endsWith = function(str) { var lastIndex = this.lastIndexOf(str); return (lastIndex !== -1) && (lastIndex + str.length === this.length); }