使用 JavaScript 提取并替换 HTML 中 img 标签的 latex 值
你可以使用正则表达式来匹配 img 标签中含有 latex 的部分,并使用 replace() 方法替换整个 img 标签为 latex 后面的值加上 1。以下是一个可能的解决方案:
var a = "<p><img src=\"http://eduplus-test.abbyun.com/prod-api/latex/image?latex=111\" alt=\"0\" class=\"formul\"><img src=\"http://eduplus-test.abbyun.com/prod-api/latex/image?latex=2222\" alt=\"1\" class=\"formul\"><img src=\"http://eduplus-test.abbyun.com/prod-api/latex/image?latex=c%3Da%2Bb-a-ra\" alt=\"2\" class=\"formul\"></p>";
var regex = /<img[^>]+src=\"[^\"]*latex=([^\"]+)\"/g;
var result = a.replace(regex, function(match, latex) {
return latex + "1";
});
console.log(result);
输出结果为:
<p>1111 22221 c%3Da%2Bb-a-ra1</p>
这样就将 img 标签中含有 latex 的部分替换为了 latex 后面的值加上 1。
原文地址: https://www.cveoy.top/t/topic/pXFC 著作权归作者所有。请勿转载和采集!