regex - 匹配模式多行 Integromat
问题描述
我想将多行中的特定模式与 Integromat 中的匹配模式进行匹配。语言是 ECMAScript (JavaScript) FLAVOR。
Salutation: Mr. x Mx. Mrs.
或者看起来像这样:
Salutation: Mr. Mx. x Mrs.
我想在 之后获取字符串x\s
,以提取Mr.
Mx.
或Mrs.
。
目前,我在这一点上,但它只有在 x 之前才匹配Mr.
Salutation:\s(x\s(.*?)[\s])
我需要如何更改它?提前致谢!
解决方案
You might use a capture group with an alternation to match either a single occurrence of Mr. Mrs. or Mx.
\bSalutation:.*?\sx\s(M(?:rs?|x)\.)
The pattern matches:
\bSalutation:
Match literally.*?\sx\s
Match any char as least as possible till the first occurrence of x between whitespace chars(
Capture group 1 (in the example referred to asm[1]
)M(?:rs?|x)\.
MatchM
followed by either r with optional s or x and then a dot
)
Close group 1
const regex = /\bSalutation:.*?\sx\s(M(?:rs?|x)\.)/;
[
"Salutation: Mr. x Mx. Mrs.",
"Salutation: Mr. Mx. x Mrs.",
"Salutation: Mr. Mx. x Mr.",
].forEach(s => {
const m = s.match(regex);
if (m) {
console.log(m[1]);
}
});
If you want to match all of the occurrences after the x, and a lookbehind is supported in the Javascript version:
(?<=\bSalutation:.*\sx\s.*)\bM(?:rs?|x)\.
推荐阅读
- php - 使用 CodeiGniter 格式化 json
- java - Spring boot oauth2 响应,重定向 url 作为底层服务而不是代理(网关)
- java - MongoDB性能测试-基本理解-Java
- html - 如何创建动画边框底部样式?
- blazor - Blazor wasm 在哪里保存 UI 模型和业务逻辑
- python - Python循环不完全遵守设定规则
- python - 如果使用 q1/median/q3,则在 plotly boxplot 中显示名称
- php - 来自 xampp 的 Phpinfo() 不显示 php-redis 扩展 (UBUNTU 20.04)
- amazon-web-services - 面临错误 WebDriverException:消息:未知错误:Chrome 无法启动:使用机器人框架在 aws linux 中异常退出
- typescript - 我可以使用 SPFX 获取列表中的所有项目...如何修改我的代码以获取特定视图?