Хотите получить значение из строки - PullRequest
0 голосов
/ 14 июля 2020

У меня есть такая строка.

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

1 Ответ

0 голосов
/ 14 июля 2020

Что-то вроде этого?

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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...