隔离非贪婪的子串?
Isolate sub-string non-greedy?
给定的字符串如:
foo/**hello**/baré
foo/crazy/**hello**/baré
**hello**/crazy/baré
对于每一行,如何通过 JS 隔离 hello
(**
之间的文本)?(正则表达式?搜索)
(每行需要的输出是hello
)
到目前为止,我在那里失败了:.*?\*\*(.*?)\*\*.*?
=> </code> 给我:</p>
<pre><code>hello/baré
hello/baré
hello/crazy/baré
您可以使用匹配除 *:
以外的任何字符的表达式
const inputs = [
'foo/**hello**/baré',
'foo/crazy/**hello**/baré',
'**hello**/crazy/baré'
];
inputs.forEach(input => {
const matches = input.match(/.*\*\*([^*]+)\*\*.*/);
console.log(matches[1]);
});
一句话,就用\w
:
\*+(\w+)\*+
如果你特别想要 2 *
就做
\*{2}(\w+)\*{2}
如果您有一个句子或一个段落或混合字符,请搜索除开始和结束分隔符以外的字符:
\*{2}([^*]+)\*{2}
给定的字符串如:
foo/**hello**/baré
foo/crazy/**hello**/baré
**hello**/crazy/baré
对于每一行,如何通过 JS 隔离 hello
(**
之间的文本)?(正则表达式?搜索)
(每行需要的输出是hello
)
到目前为止,我在那里失败了:.*?\*\*(.*?)\*\*.*?
=> </code> 给我:</p>
<pre><code>hello/baré
hello/baré
hello/crazy/baré
您可以使用匹配除 *:
以外的任何字符的表达式const inputs = [
'foo/**hello**/baré',
'foo/crazy/**hello**/baré',
'**hello**/crazy/baré'
];
inputs.forEach(input => {
const matches = input.match(/.*\*\*([^*]+)\*\*.*/);
console.log(matches[1]);
});
一句话,就用\w
:
\*+(\w+)\*+
如果你特别想要 2 *
就做
\*{2}(\w+)\*{2}
如果您有一个句子或一个段落或混合字符,请搜索除开始和结束分隔符以外的字符:
\*{2}([^*]+)\*{2}