Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正则表达式在HTML中以粗体显示匹配项_Javascript_Regex - Fatal编程技术网

Javascript 正则表达式在HTML中以粗体显示匹配项

Javascript 正则表达式在HTML中以粗体显示匹配项,javascript,regex,Javascript,Regex,我试图编写一个函数,将字符串数组与HTML代码字符串相匹配。然后,它会在比赛周围放置加粗和/或加粗标记。现在,如果我将字符串与常规文本进行匹配,函数将类似于: function boldText(matches, text) { var pattern = new RegExp(matches.join("|"),"g"); text.replace(pattern,"<b>"+__what to write here?__+ "</b>" } 函数粗体

我试图编写一个函数,将字符串数组与HTML代码字符串相匹配。然后,它会在比赛周围放置加粗和/或加粗标记。现在,如果我将字符串与常规文本进行匹配,函数将类似于:

function boldText(matches, text)
{
    var pattern = new RegExp(matches.join("|"),"g");
    text.replace(pattern,"<b>"+__what to write here?__+ "</b>"
}
函数粗体文本(匹配项,文本)
{
var pattern=newregexp(matches.join(“|”),“g”);
text.replace(模式“+”在这里写什么?“
}
但是,由于它是一个HTML文件,我不想加粗小于号和大于号之间的任何内容。只有HTML标记之外的内容才应该加粗。正则表达式可能是什么

重申一下,我正在寻找一个正则表达式,当且仅当匹配不在“小于号”+任意数量的字符和任意数量的字符+大于号之间时才匹配

呃,没有人回答我:(

你应该使用以下模式:

text.replace(pattern, '<b>$&</b>');
text.replace(模式,$&');

不要用正则表达式解析HTML!@Tibos是否有证据表明
标记已弃用?@VisioN感谢您指出这一点。
B
元素的含义似乎已经改变(而不是弃用了元素):用正则表达式解析HTML有什么问题?