【问题标题】:Equivalent for specific php preg_replace in javascript等效于 javascript 中的特定 php preg_replace
【发布时间】:2012-11-19 00:36:10
【问题描述】:

在 JS 中这个特定的 php preg 替换是否有等价物?

preg_replace('/[\n]{2,}/', "\n", "Hi,\nHow are you?\n\n\nI am just testing");

谢谢!

【问题讨论】:

  • 您在 Google 或 StackOverflow 上搜索过吗?正则表达式在所有语言中几乎是相同的,您只需要搜索如何将正则表达式与 JS 一起使用。我想说的有点懒惰的问题......

标签: php javascript preg-replace


【解决方案1】:

基本相同,但您必须提供g 标志:

var theString = "Hi,\nHow are you?\n\n\nI am just testing";

theString = theString.replace(/\n{2,}/g, '\n');

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 2010-09-29
    • 2015-01-16
    • 1970-01-01
    • 2010-12-29
    • 2010-10-21
    • 2013-07-15
    • 1970-01-01
    相关资源
    最近更新 更多