Extract sub string from last in a string JS

This question already has an answer here:

  • endsWith in JavaScript 29 answers

  • You'll want to use the Javascript string method .substr() combined with the .length property.

    function isDepreciated(var id)
    {
        var id = "string value - depreciated";
        var lastdepreciated = id.substr(id.length - 13); // => "- depreciated"
        //return true or false check for true or flase
    }
    

    This gets the characters starting at id.length - 13 and, since the second argument for .substr() is omitted, continues to the end of the string.


    在你的JS中添加下面的代码

    function isDepreciated(string){
       return  /(-depreciated)$/.test(string);
    }
    

    function isDepreciated(S) {
        var suffix = "- depreciated";
        return S.indexOf(suffix, S.length - suffix.length) !== -1;
    }
    
    链接地址: http://www.djcxy.com/p/83890.html

    上一篇: 获取另一个字符串中最后一次出现的字符串

    下一篇: 从字符串JS中提取最后一个子字符串