javascript - 想要从字符串中获取值
问题描述
我有一个这样的字符串。
var test = "the email body text is <p>the email test is the email tes is the email.</p>
<p><span id="xXmergefield selnameXx5" class="tag mergefield selname" style="background-color: rgba(0, 0, 255, 0.75);" contenteditable="false" **data-replacement="{MERGEFIELD SailName}"**>Sail Name<em id="xXmergefield selnameXx5-remover" class="fas fa-times remover"></em></span></p>"
为了保存,我希望变量测试为,
var test = "the email body text is <p>the email test is the email tes is the email.</p>{MERGEFIELD SailName}".
代替 - -
<p><span id="xXmergefield selnameXx5" class="tag mergefield selname" style="background-color: rgba(0, 0, 255, 0.75);" contenteditable="false" data-replacement="{MERGEFIELD SailName}">Sail Name<em id="xXmergefield selnameXx5-remover" class="fas fa-times remover"></em></span></p>
-------- 我只想要属性数据替换的值- {MERGEFIELD SailName}。
最终结果必须是
var test = "the email body text is <p>the email test is the email tes is the email.</p>{MERGEFIELD SailName}".
解决方案
像这样的东西?
var input = 'the email body text is <p>the email test is the email tes is the email.</p><p><span id="xXmergefield selnameXx5" class="tag mergefield selname" style="background-color: rgba(0, 0, 255, 0.75);" contenteditable="false" data-replacement="{MERGEFIELD SailName}">Sail Name<em id="xXmergefield selnameXx5-remover" class="fas fa-times remover"></em></span></p>'
var matches = input.match(/<p><span(.*?)<\/span><\/p>/g);
for(var i=0;i<matches.length;i++){
var tag = matches[i].match(/data-replacement="(.*?)">/)[1];
input = input.replace(matches[i],tag);
}
console.log(input);
推荐阅读
- node.js - 如何获取 x 数量的消息
- http - CORS cookie 未保存在浏览器中
- html - 有问题
- c# - 来自用户控件的 MainForm 上的动画网格边距
- python - 媒体文件未在 cpanel 上的 django 应用程序中提供
- sql - Hive 查询:匹配字符串数组中的列值以制作标志
- c# - 序列化包含列表的对象时 ProtoBuf-net 无效行为
- vba - 防止用户在加载时向 Esc 发送垃圾邮件以关闭表单?
- android - 将应用程序限制为小屏幕,2.7 英寸 (240 x 320) 像素分辨率
- php - 不再支持 PayPal IPN 和 fsockopen?