【问题标题】:JavaScript's regexp - new lines and empty spaces at the end of a string [duplicate]JavaScript正则表达式 - 字符串末尾的新行和空格[重复]
【发布时间】:2012-03-13 15:10:06
【问题描述】:

可能重复:
Trim spaces from start and end of string

我想找到一种方法来达到以下结果:

"I am\n a string \n\n  \n \n".replace(/\n{1,}$/g,""); // "I am\n a string"

最好的方法是什么?

【问题讨论】:

    标签: javascript regex


    【解决方案1】:
    "I am\n a string \n\n  \n \n".replace(/\s+$/,""); // "I am\n a string"
    

    应该做的工作。

    \s 是一个空格字符,包括空格、制表符、换行符

    +{1,} 是同一个量词

    【讨论】:

    • \s 字符类包括tab换行换页回车 和 空格.
    【解决方案2】:

    试试这个

       "I am a string \n\n  \n \n".replace(/\s+$/g, "")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      相关资源
      最近更新 更多