假设有一个包含以下内容的textarea文本:
On date at 3:16 PM, Sir Voldemort wrote:
> Sweet Harry!
> Let's see what's gonna happen.
>
>
> On date at 3:11 PM, wrote:
>
>> Awesome!
>>
>>
>> --
>> Random: string or sentence here, it doesn't matter
>> Another random line with e.g. a URL: http://www.jkrowling.com/
>>
>
>
>
> --
> **********************************************
> ABCABCCAB
> **********************************************
>
Quote
“Efe iska?”
--
Random2-1
Random2-2
**********************************************
ABCABCABC
**********************************************
现在,如果出现一行只有两个短划线--
(并且没有,前面有一个或多个>
),则该行和以下两行应该被删除。
就像那样:
On date at 3:16 PM, Sir Voldemort wrote:
> Sweet Harry!
> Let's see what's gonna happen.
>
>
> On date at 3:11 PM, wrote:
>
>> Awesome!
>>
>>
>>
>
>
>
> **********************************************
>
Quote
“Efe iska?”
**********************************************
ABCABCABC
**********************************************
我已经开始使用以下代码来检测--
的上一行以启动另一个substr
,但是更确定一种更简单的正则表达式解决方案吗?
var msg = document.getElementById('msg'); // wrapped textarea around the text with ID msg
msg.value = msg.value.substr(0, msg.value.substr(0, msg.value.match('--\n').index).lastIndexOf('\n'))
答案 0 :(得分:1)
这应该有用。
str.replace(/\n>*\s*--\n.*\n.*/g, '')
结果:
On date at 3:16 PM, Sir Voldemort wrote:
> Sweet Harry!
> Let's see what's gonna happen.
>
>
> On date at 3:11 PM, wrote:
>
>> Awesome!
>>
>>
>>
>
>
>
> **********************************************
>
Quote
“Efe iska?”
**********************************************
ABCABCABC
**********************************************