Trim spaces from start and end of string

前端 未结 14 1317
情歌与酒
情歌与酒 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:52

    Here, this should do all that you need

    function doSomething(input) {
        return input
                  .replace(/^\s\s*/, '')     // Remove Preceding white space
                  .replace(/\s\s*$/, '')     // Remove Trailing white space
                  .replace(/([\s]+)/g, '-'); // Replace remaining white space with dashes
    }
    
    alert(doSomething("  something with  some       whitespace   "));
    
    0 讨论(0)
  • 2020-11-28 02:56

    jQuery.trim(" hello, how are you? ");

    :)

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