我有这个:
const str = "你好 **Tom**,现在是 **19h25**。这里是 **John1** 在 **过去 2 小时** 发现的 **19** 东西"
我需要在内容中有数字的地方找到所有用双星号包围的内容。
我希望 str.match(regex)
返回 ['19h25', '19', 'last 2 hours', 'john1']
。但不是 **Tom**
因为内容中没有数字。
我尝试过像这样的正则表达式 /\*{2}(.*\d)\*{2}/g
但它不起作用。
编辑: **
*
最佳答案
你可以使用
/\*{2}([^\d*]*\d[^*]*)\*{2}/g
参见 regex demo
详情
\*{2}
-**
子串([^\d*]*\d[^*]*)
- 第 1 组:[^\d*]*
- 除了数字和*
之外的 0+ 个字符
\d
- 一个数字[^*]*
-*
以外的 0+ 个字符
\*{2}
-**
子串
JS 演示:
const str = "hello **Tom**, it's currently **19h25**. Here is **19** things found during the **last 2 hours** by **John1**";
const rx = /\*{2}([^\d*]*\d[^*]*)\*{2}/g;
let m, res = [];
while (m = rx.exec(str)) {
res.push(m[1]);
}
console.log(res);
// or a one liner
console.log(str.match(rx).map(x => x.slice(2).slice(0, -2)));
关于javascript - 正则表达式查找包含双星号包围的数字的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51956945/