我有这个字符串:
var string = "From: Jeremy<br />
Sent: 22 Apr 2016 12:08:03</br />
To: Mark<br />
Subject: Another test email<br /><br />
Hi Mark,
<br />
I'm sending this email as a way to test the email!
<br />
Cheers,
<br />
Jeremy"
我想从开头到“主题”行之后拼接该字符串,以便获得电子邮件正文内容。
到目前为止我已经尝试过:
string.substring(string.lastIndexOf('Subject'), string.length - 1)
但此选项也会从字符串中返回主题行。
有我可以使用的正则表达式库吗?
最佳答案
使用string.replace
你可以这样做:
var body = string.replace(/^[\s\S]*\s+Subject: [^\n]*\n+/, '')
代码:
var string = `From: Jeremy<br />
Sent: 22 Apr 2016 12:08:03</br />
To: Mark<br />
Subject: Another test email<br /><br />
Hi Mark,
<br />
I'm sending this email as a way to test the email!
<br />
Cheers,
<br />
Jeremy`
var body = string.replace(/^[\s\S]*\s+Subject: [^\n]*\n+/, '')
document.writeln("<pre>" + body + "</pre>")
[\s\S]*
匹配 0 个或多个任何字符(包括换行符)。其后的 Subject:
行也被删除。
输出:
"Hi Mark,
<br />
I'm sending this email as a way to test the email!
<br />
Cheers,
<br />
Jeremy"
关于javascript - 正则表达式删除字符串中字符的开头到结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36895952/