将可在Chrome中使用的Regex表达式转换为可在Firefox中使用 [英] Converting a Regex Expression that works in Chrome to work in Firefox

查看:138
本文介绍了将可在Chrome中使用的Regex表达式转换为可在Firefox中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Regex表达式,可以在chrome中使用,但在Firefox中不起作用. SyntaxError: invalid regexp group

I have this Regex Expression that works in chrome but doesn't not work in Firefox. SyntaxError: invalid regexp group

它与lookbehinds有关,而Firefox不支持这些.我需要这个才能在Firefox中工作,有人可以帮我转换吗 这样它就可以在Firefox中运行,并且还可以过滤掉标签吗?

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(`(?!<|>|/|&amp|_)(?<!</?[^>]*|&[^;]*)(${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">.

推荐答案

您可以尝试在不使用否定性背后的情况下解决问题:相反,做对您不想要的事情.如果您的回调中有该文件,请确保不要突出显示.

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>`);

这篇关于将可在Chrome中使用的Regex表达式转换为可在Firefox中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆