问题
I have this Regex Expression that works in chrome but doesn't not work in Firefox. SyntaxError: invalid regexp group
It has something to do with lookbehinds and Firefox does not support these. I need this to work in Firefox can some one help me convert this so it works in Firefox and filters out the tags as well?
return new RegExp(`(?!<|>|/|&|_)(?<!</?[^>]*|&[^;]*)(${term})`, 'gi');
};
searchTermsInArray.forEach(term => {
if (term.length) {
const regexp = this.regexpFormula(term);
newQuestion.qtiData.prompt = newQuestion.qtiData.prompt.replace(regexp, match => {
return `<span class="highlight">${match}</span>`;
});```
In chrome it filters out the html tags and returns the search term with a <span class="highlight">.
回答1:
You could try to solve things without using a negative lookbehind: do the opposite, match what you do not want as well. If it is there in your callback, then make sure to not highlight.
Note: I am not sure what the negative lookahead is accomplishing at the beginning, as you could easily make sure that the search term doesn't start with the listed values and that would yield the same result, so I am letting this part aside.
let regexp = new RegExp(`(&[^;]*|</?[^>]*)?(${term})`, 'gi');
haystack.replace(regexp, (match, ignore, term) => ignore ? match : `<span class="highlight">${term}</span>`);
来源:https://stackoverflow.com/questions/56759795/converting-a-regex-expression-that-works-in-chrome-to-work-in-firefox