javascript - 正则表达式 2 个字符串中的数字
问题描述
如何从该字符串中提取中间的 2 个数字Price Our:
和以下 数字?|
"Delivery: Price => 30.00 - 45.00 | Price Our: 1900.00 => 1800.00 | Delivery: Contact => 3 - 4 Days | "
我试过(Price Our:.*?(?=\|))
但这就是我的正则表达式知识的限制
解决方案
You could use 2 capture groups:
\bPrice Our: (\d+(?:\.\d+)?) => (\d+(?:\.\d+)?) \|
The pattern matches:
\bPrice Our:
A word boundary to prevent a partial match, then matchPrice Our:
(\d+(?:\.\d+)?)
Capture group 1, match a number with an optional decimal part=>
Match literally(\d+(?:\.\d+)?)
Capture group 2, match a number with an optional decimal part\|
Match|
const s = "Delivery: Price => 30.00 - 45.00 | Price Our: 1900.00 => 1800.00 | Delivery: Contact => 3 - 4 Days | ";
const regex = /\bPrice Our: (\d+(?:\.\d+)?) => (\d+(?:\.\d+)?) \|/;
const match = s.match(regex);
if (match) {
console.log(match[1]);
console.log(match[2]);
}
In case the digits are at the end of the string, and there is no |
following, you can account for that using an alternation to assert the end of the string:
\bPrice Our: (\d+(?:\.\d+)?) => (\d+(?:\.\d+)?)(?: \||$)
推荐阅读
- java - java.net HttpURLConnection 内容类型不匹配
- reactjs - 功能组件是否会导致内存问题?
- string - 如何使用 PySpark 拆分字符串数据并在表中加载数据
- android - 我可以从应用程序调用授予权限 ADB 命令吗
- github - 我在谷歌搜索中找不到我的 github 帐户
- pandas - pandas:如何使用 predict_proba 获取每个客户的概率
- oracle - 什么是预言机事件记录?
- laravel - 在 Lumen 包开发中未触发包级 Composer
- java - 在 Java 编译时生成枚举
- excel - 工作簿是 VBA Excel 中的类还是集合?