如何使用正则表达式获取两个分隔符之间的内容?例如,我希望得到两个|
之间的东西。例如,对于此输入:
|This is the text I want|
它应该返回:
This is what I want
我已尝试/^|(.*)$|/
,但返回This is what I want |
而非This is what I want
(最后不应该|
)
答案 0 :(得分:3)
/\|(.*?)\|/
例如,使用JavaScript:
var s = '| This is what I want |';
var m = s.match(/\|\s*(.*?)\s*\|/);
m[1]; // => "This is what I want"
见这里的例子: https://regex101.com/r/qK6aG2/2