![](/img/trans.png)
[英]Find and replace opening and closing HTML tag with specific class using Regex
[英]Find the word and replace with html tag using regex
您当前的模式与插入符号匹配,后跟一个空格字符(空格,制表符,换行符等),但是您想要匹配一个插入符号,后跟一个或多个用修饰符包裹的字符,因为您的字符串在TeX中。
/\^(?:([\w\d])|\{([\w\d]{2,})\})/g
现在,使用str = str.replace(/\\^(?:([\\w\\d])|\\{([\\w\\d]{2,})\\})/g, "<sup>$1</sup>");
应该做的工作。
您可以从此表达式中创建一个更通用的函数,该函数可以将带有特定字符前缀的字符包装到特定标签中。
function wrapPrefixed(string, prefix, tagName) {
return string.replace(new RegExp("\\" + prefix + "(?:([\\w\\d])|\\{([\\w\\d]{2,})\\})"), "<" + tagname + ">$1</" + tagname + ">");
}
例如,调用wrapPrefixed("1_2 + 4_{3+2}", "_", "sub");
结果为1<sub>2</sub> + 4<sub>3+2</sub>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.