Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  115] [ 4]  / answers: 1 / hits: 19185  / 11 Years ago, thu, september 12, 2013, 12:00:00

I have a web application. In one of the pages, I go all over the HTML element IDs wether one of them ends with a specified string or not. Every JS functions work on the page but endsWith function doesn't work. I really didn't understand the matter. Can anyone help?



var str = To be, or not to be, that is the question.;
alert(str.endsWith(question.));


The above simple JS code doesn't work at all?


More From » javascript

 Answers
34

As said in this post http://rickyrosario.com/blog/javascript-startswith-and-endswith-implementation-for-strings/



var str = To be, or not to be, that is the question.;
function strEndsWith(str, suffix) {
return str.match(suffix+$)==suffix;
}
alert(strEndsWith(str,question.));


this will return true if it ends with provided suffix.



JSFIDDLE



EDIT



There is a similar question asked before check it here



the answer says



var str = To be, or not to be, that is the question$;
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
alert(str.endsWith($));

[#75730] Wednesday, September 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 11, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;