Trimming whitespace from the END of a string only, with jQuery

前端 未结 2 656
故里飘歌
故里飘歌 2021-02-07 05:28

I know of the jQuery $.trim() function, but what I need is a way to trim whitespace from the END of a string only, and NOT the beginning too.

So

  str =\         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 06:27

    You can use a regex:

    str = str.replace(/\s*$/,"");
    

    It says replace all whitespace at the end of the string with an empty string.

    Breakdown:

    • \s* : Any number of spaces
    • $ : The end of the string

    More on regular expressions:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

提交回复
热议问题