提问者:小点点

想从字符串中获取值


我有一根这样的绳子。

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>"

为了保存,我想要变量test as,

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>

-------我想要属性data-replacement only的值-{MERGEFIELD SailName}。

最终结果需要

var test = "the email body text is <p>the email test is the email tes is the email.</p>{MERGEFIELD SailName}".

共2个答案

匿名用户

像这样的?

null

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 test = "the email body text is <p>the email test is the email tes is the email.</p>";

input = input.replace(test,'');
test+=input.match(/data-replacement="(.*?)">/)[1]
console.log(test);

//{MERGEFIELD SailName}

匿名用户

null

    const 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>`
     
     
    const div = document.createElement('div');
          div.innerHTML = test.trim();
    const attribute = div.querySelector('.tag').getAttribute('**data-replacement');
    const result = `${div.innerHTML}${attribute}`;

    console.log(result)