我正试图弄清楚如何像这样操作字符串:
string abc = "<p> Hello world <em> How are you? </em> abc <em> You there </em> </p>"
进入
string def = "<p> Hello world </> <em> How are you? </> <p> abc </> <em> You there </>"
解释:对于每个标记,结束标记都将是</>
this,我不允许像在普通HTML中那样使用嵌套标记。这就是为什么我的字符串def
在每个打开标记之后都有</>
的原因。
<p> hello world </> (tag closed) <em> how are you? </> (em tag closed) <p> abc </> (p tag closed) <em> You there </> (em tag closed)
我尝试在regex上使用exec来查找HTML结束标记,并用<>。它起了作用,但不幸的是,我不知道如何使用打开的标签。
我还尝试使用空格"拆分字符串并在数组中循环。但运气不好。
有人能引导我通过这个吗?
在这种情况下使用regex会非常困难,我们可以将数组拆分到" "
上,然后简单地在其上循环,并决定字符串和标记的当前状态。
function changeHTMLEndTags(html_string = "") {
// adding a space before '<' and after '>'
html_string = html_string.replace(/(<)/g, ' <').replace(/(>)/g, '> ');
// removing multiple spaces introduced due to above operation
// and triming the start and end of the string
html_string = html_string.replace(/ss+/g, ' ').trim();
html_string_words = html_string.split(" ");
final_html_string = "";
last_opening_tag = null;
for(i = 0; i < html_string_words.length; ++i) {
current_word = html_string_words[i];
// if the current_word is an opening tag
if (current_word.match(/<w+>/)) {
if (last_opening_tag === null) {
final_html_string += current_word + " ";
last_opening_tag = current_word;
}
else {
final_html_string += "</>" + " " + current_word + " ";
last_opening_tag = current_word;
}
}
// if the current_word is a closing tag
else if (current_word.match(/</w+>/)) {
if (last_opening_tag !== null) {
final_html_string += "</>" + " ";
last_opening_tag = null;
}
else {
// do nothing with the current_word,
// let it get swallowed, we want to drop the dangling closing tags
}
}
// else the current_word is a normal word
else {
if (last_opening_tag !== null) {
final_html_string += current_word + " ";
}
else {
// I took assumption here,
// that if we don't have any opening tag, we will use `<p>`
final_html_string += "<p>" + " " + current_word + " ";
last_opening_tag = "<p>";
}
}
}
return final_html_string.trim();
}
abc = "<p> Hello world <em> How are you? </em> abc <em> You there </em> </p>";
console.log(changeHTMLEndTags(abc));
abc = "<p> Hello world<em> How are you? </em> abc <em> You there </em> </p>";
console.log(changeHTMLEndTags(abc));
可能有一种更理想的方法可以做到这一点,但它可以用</>
替换</em>
和</p>
,这似乎是您的目标(尽管您的问题和示例输出不一致(。
let string = "<p> Hello world <em> How are you? </em> abc <em> You there </em> </p>";
let def = string.replace(/</(em|p)>/g, "</>");
console.log(def);