Trim spaces from start and end of string

前端 未结 14 1439
情歌与酒
情歌与酒 2020-11-28 01:56

I am trying to find a way to trim spaces from the start and end of the title string. I was using this, but it doesn\'t seem to be working:

title = title.repl         


        
14条回答
  •  温柔的废话
    2020-11-28 02:51

    Here is some methods I've been used in the past to trim strings in js:

    String.prototype.ltrim = function( chars ) {
        chars = chars || "\\s*";
        return this.replace( new RegExp("^[" + chars + "]+", "g"), "" );
    }
    
    String.prototype.rtrim = function( chars ) {
        chars = chars || "\\s*";
        return this.replace( new RegExp("[" + chars + "]+$", "g"), "" );
    }
    String.prototype.trim = function( chars ) {
        return this.rtrim(chars).ltrim(chars);
    }
    

提交回复
热议问题