对于输入
*something here*another stuff here
我想匹配两个星号 (*) 之外的所有内容。
正则表达式后的预期输出:
another stuff here
我想出了如何匹配 (*) /(?<=\*)(.*)(?=\*)/
内部的所有内容,但我无法匹配外部的所有内容。注意到我不想匹配 *.
答案 0 :(得分:4)
您可以删除星号之间的子字符串并修剪以下字符串:
s.replace(/\*[^*]*\*/g, '').trim()
s.replace(/\*.*?\*/g, '').trim()
参见regex demo。
详情
\*
- 星号[^*]*
- 除星号外的任何零个或多个字符.*?
- 除换行符以外的任何零个或多个字符,尽可能少(注意:如果您使用 .*
,如果字符串星号之间有多个子串)\*
- 星号查看 JavaScript 演示:
console.log("*something here*another stuff here".replace(/\*[^*]*\*/g, '').trim())
// => another stuff here
答案 1 :(得分:1)
您可以使用 split
* anything *
字符串,然后 join
字符串以获得结果。
const mystring = "*something here*another stuff here";
const result = mystring.split(/[*].*[*]/).join("");
console.log(result);