JavaScript or jQuery string ends with utility function

后端 未结 8 1788
遥遥无期
遥遥无期 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
    
    0 讨论(0)
  • 2020-12-24 01:01

    You can do 'hello world'.slice(-5)==='world'. Works in all browsers. Much faster than regex.

    0 讨论(0)
提交回复
热议问题