JavaScript or jQuery string ends with utility function

后端 未结 8 1838
遥遥无期
遥遥无期 2020-12-23 23:56

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

8条回答
  •  青春惊慌失措
    2020-12-24 00:52

    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
    

提交回复
热议问题