我们在Safari浏览器中使用我们正在使用的regex时遇到了一些问题.下面是正则表达式:

/(?<=type: ).*./g

Safari无法处理此问题,因此我们将其更改为:

/(?:)(type: ).*./g

问题是,现在对于Chrome,它不能返回正确的值.有没有办法让一个正则表达式同时适用于Chrome和Safari,并获得相同的响应?因此,我们希望得到的回应是:

BUSINESS
ACC
false

Code Example

const query = "{\n          testObject(testInput : {\n            id: \"id-13\",\n            life: ACC,\n            type: BUSINESS,\n            promo: [],\n            isvalid: false\n          })}\n      }"

const pattern = /\(([^)]+)\)/g;
const result = query.match(pattern) ? query.match(pattern).map((v) => v.trim().replace(/^\(|\)$/g, ''))[0] : null;

/* DOES NOT WORK IN SAFARI */
const queryTypePattern = /(?<=type: ).*./g;
const queryLifePattern = /(?<=life: ).*./g;
const queryIsValidPattern = /(?<=isvalid: ).*./g;

const queryType = result.match(queryTypePattern) ? result.match(queryTypePattern).map((v) => v.trim().replace(',', ''))[0] : null;
const queryLife = result.match(queryLifePattern) ? result.match(queryLifePattern).map((v) => v.trim().replace(',', ''))[0] : null;
const queryIsValid = result.match(queryIsValidPattern) ? result.match(queryIsValidPattern).map((v) => v.trim().replace(',', ''))[0] : null;

console.log('#### RETURNS THE CORRECT OUTPUT IN CHROME: ');
console.log(queryType);
console.log(queryLife);
console.log(queryIsValid);


/* WORKS IN SAFARI */
const queryTypePattern2 = /(?:type: ).*./g;
const queryLifePattern2 = /(?:life: ).*./g;
const queryIsValidPattern2 = /(?:isvalid: ).*./g;

const queryType2 = result.match(queryTypePattern2) ? result.match(queryTypePattern2).map((v) => v.trim().replace(',', ''))[0] : null;
const queryLife2 = result.match(queryLifePattern2) ? result.match(queryLifePattern2).map((v) => v.trim().replace(',', ''))[0] : null;
const queryIsValid2 = result.match(queryIsValidPattern2) ? result.match(queryIsValidPattern2).map((v) => v.trim().replace(',', ''))[0] : null;

console.log('#### RETURNS THE INCORRECT OUTPUT IN CHROME: ');
console.log(queryType2);
console.log(queryLife2);
console.log(queryIsValid2);

推荐答案

你可以在没有全局标志的情况下捕获.*个并进行匹配.

/(?:type: )(.*)/

通过这种方式,它返回一个由两个字符串组成的数组:

  • 完全匹配的值(例如:type: BUSINESS)
  • 捕获的组(例如:BUSINESS)

我们需要第二个.

const query = "{\n          testObject(testInput : {\n            id: \"id-13\",\n            life: ACC,\n            type: BUSINESS,\n            promo: [],\n            isvalid: false\n          })}\n      }"

const pattern = /\(([^)]+)\)/g;
const result = query.match(pattern) ? query.match(pattern).map((v) => v.trim().replace(/^\(|\)$/g, ''))[0] : null;

const queryTypePattern2 = /(?:type: )(.*)/;
const queryLifePattern2 = /(?:life: )(.*)/;
const queryIsValidPattern2 = /(?:isvalid: )(.*)/;

const queryType2 = result.match(queryTypePattern2) ? result.match(queryTypePattern2).map((v) => v.trim().replace(',', ''))[1] : null;
const queryLife2 = result.match(queryLifePattern2) ? result.match(queryLifePattern2).map((v) => v.trim().replace(',', ''))[1] : null;
const queryIsValid2 = result.match(queryIsValidPattern2) ? result.match(queryIsValidPattern2).map((v) => v.trim().replace(',', ''))[1] : null;

console.log(queryType2);
console.log(queryLife2);
console.log(queryIsValid2);

由于该解决方案不包含lookback,因此它可以同时适用于两种浏览器.

Javascript相关问答推荐

获取加载失败:获取[.]添加时try 将文档添加到Firerestore,Nuxt 3

如何访问Json返回的ASP.NET Core 6中的导航图像属性

如何将连续的十六进制字符串拆分为以空间分隔的十六进制块,每个十六进制块包含32个二元组?

使用i18next在React中不重新加载翻译动态数据的问题

我们如何从一个行动中分派行动

在服务器上放置了Create Reaction App Build之后的空白页面

虚拟滚动实现使向下滚动可滚动到末尾

当id匹配时对属性值求和并使用JavaScript返回结果

在不删除代码的情况下禁用Java弹出功能WordPress

处理app.param()中的多个参数

使用线性插值法旋转直线以查看鼠标会导致 skip

Cherrio JS返回父div的所有图像SRC

不协调嵌入图片

如何在尚未创建的鼠标悬停事件上访问和着色div?

我为什么要使用回调而不是等待?

MUI迷你图:如何将$符号添加到MUI迷你图中的工具提示数据

我怎样才能点击一个元素,并获得一个与 puppeteer 师导航页面的URL?

使用Java脚本在div中创建新的span标记

打字脚本中的函数包装键入

将promise列表拆分到组.按组并行和顺序执行所有promise