Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript or jQuery string ends with utility function

what is the easiest way to figure out if a string ends with a certain value?

like image 340
Jon Erickson Avatar asked Jul 07 '09 22:07

Jon Erickson


People also ask

What indicates end of the string in Javascript?

The endsWith() method determines whether a string ends with the characters of another string, returning true or false as appropriate. This method is case-sensitive.

What is $$ jQuery?

jQuery is an object provided by jQuery. $ is another, which is just an alias to jQuery . $$ is not provided by jQuery. It's provided by other libraries, such as Mootools or Prototype. js.


Video Answer


2 Answers

you could use Regexps, like this:

str.match(/value$/) 

which would return true if the string has 'value' at the end of it ($).

like image 56
cloudhead Avatar answered Oct 22 '22 07:10

cloudhead


Stolen from prototypejs:

String.prototype.endsWith = function(pattern) {     var d = this.length - pattern.length;     return d >= 0 && this.lastIndexOf(pattern) === d; };  'slaughter'.endsWith('laughter'); // -> true 
like image 45
Luca Matteis Avatar answered Oct 22 '22 07:10

Luca Matteis