【发布时间】:2020-10-22 05:32:45
【问题描述】:
我有这样的 HTML — BLAH BLAH BLAH <sup>x</sup> — 在 <th> 中。
我正在尝试将<sup>x</sup> 替换为(x)。
这些都是我尝试过的不同方法。 insideSup 是字母。
newText = $(tableRow).find("th").eq(tdIndex)
.text().replace("<sup>(.*?)</sup>", " ") + "(" + insideSup + ")";
newText = $(tableRow).find("th").eq(tdIndex)
.html().replace("<sup>(.*?)</sup>", " ") + "(" + insideSup + ")";
newText = $(tableRow).find("th").eq(tdIndex)
.find("sup").html().replace("/<sup>/g", " ") + "(" + insideSup + ")";
newText = $(tableRow).find("th").eq(tdIndex)
.find("sup").html().replace("<sup>(.*?)</sup>", " ") + "(" + insideSup + ")";
newText = $(tableRow).find("th").eq(tdIndex)
.text().find("sup").html().replace("<sup>(.*?)</sup>", " ") + "(" + insideSup + ")";
【问题讨论】:
-
您的所有尝试实际上都不包含正则表达式。
"<sup>(.*?)</sup>"是一个字符串,/<sup>(.*?)<\/sup>/是一个正则表达式;"/<sup>/g"是一个字符串,/<sup>/g是一个正则表达式,依此类推。replace匹配字符串字面意思。
标签: javascript html jquery regex dom