【问题标题】:Find and wrap string that contains line break查找并换行包含换行符的字符串
【发布时间】:2016-11-19 14:49:04
【问题描述】:

考虑以下字符串:

You look away whilst I weave needles
through my skin to seal the scars,
running repairs, not alterations.

实际上存储在数据库中的是:

You look away whilst I weave needles\r\n
through my skin to seal the scars,\r\n
running repairs, not alterations.\r\n

如何找到最后两行并将其包装在 span 标签中?例如

You look away whilst I weave needles
<span>through my skin to seal the scars,
running repairs, not alterations.</span>

目前我有:

let regex = new RegExp(stringToMatch,'m'); //I thought 'm', multiline might work?
poem = poem.replace(regex, '<span>' + stringToMatch + '</span>')

这适用于不包含任何\r\n 的字符串。

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    我建议你使用这样的东西

    var poem = 'You look away whilst I weave needles\r\nthrough my skin to seal the scars,\r\nrunning repairs, not alterations.\r\n';
    let regex = /((.+[\r\n]+)+)((.+[\r\n]+){2})/
    poem = poem.replace(regex, '$1<span>$3</span>');
    

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多