JavaScript 正则表达式替换 <p> 标签中 //{} 内的值为 <img> 标签
你可以使用正则表达式来匹配并替换字符串中的内容。下面是一个示例代码:/n/njavascript/nvar str = '<p>//{111//}//{2222//}//{c%3Da%2Bb-a-ra//}</p>';/nvar regex = //////{([^}]*)/////}/g;/nvar result = str.replace(regex, '<img src=/'http://eduplus-test.abbyun.com/prod-api/latex/image?latex=$1/' alt=/'$1/' class=/'formul/'>');/nconsole.log(result);/n/n/n这段代码中,我们使用了正则表达式 //////{([^}]*)/////}/g 来匹配 //{} 内的内容。然后,我们使用 replace 方法将匹配到的内容替换为 <img> 标签,其中 src 属性和 alt 属性的值分别为匹配到的内容。/n/n输出结果为:/n/nhtml/n<p><img src=/'http://eduplus-test.abbyun.com/prod-api/latex/image?latex=111/' alt=/'111/' class=/'formul/'><img src=/'http://eduplus-test.abbyun.com/prod-api/latex/image?latex=2222/' alt=/'2222/' class=/'formul/'><img src=/'http://eduplus-test.abbyun.com/prod-api/latex/image?latex=c%3Da%2Bb-a-ra/' alt=/'c%3Da%2Bb-a-ra/' class=/'formul/'></p>/n
原文地址: https://www.cveoy.top/t/topic/pXF2 著作权归作者所有。请勿转载和采集!