正则表达式帮助(获取无效正则表达式)

时间:2016-11-03 09:47:09

标签: javascript regex

我继承了另一个我发现无法调试的开发人员编写的函数。

function getHighlightWordsForIssueHTML(cIssue, nIssueNum, arWordsToHighlight, strStartingHTML, strReplacementHTML)
{
var nIssueStartIndex = strStartingHTML.indexOf("<issuestart" + nIssueNum + "_");
var nIssueEndIndex = strStartingHTML.indexOf("<issueend" + nIssueNum + "_");
var strRelevantPart = strStartingHTML.slice(nIssueStartIndex, nIssueEndIndex);
for (var i = 0; i < arWordsToHighlight.length; i++)
{
    var strWord = arWordsToHighlight[i];
    strRelevantPart = strRelevantPart.replace(new RegExp(strWord, 'g'),
        String.format(strReplacementHTML, strWord));
}

strStartingHTML = strStartingHTML.replaceBetween(nIssueStartIndex,
        nIssueEndIndex, strRelevantPart);
return strStartingHTML;
}

这会返回错误 strRelevantPart = strRelevantPart.replace(new RegExp(strWord, 'g'), 当它正在寻找的文本(由strWord变量捕获)包含括号时。如下例所示:

"12) months after the"

有问题的错误是:

  

未捕获的SyntaxError:无效的正则表达式:/ 12)/ /之后几个月&#39;)&#39;

你们可以帮我解决这个问题吗?

0 个答案:

没有答案