给定字符串,例如:
foo/**hello**/baré
foo/crazy/**hello**/baré
**hello**/crazy/baré
对于每一行,如何通过 JS 隔离 hello
(**
之间的文本)?(正则表达式?搜索)
(每行想要的输出是 hello
)
到目前为止,我失败了: .*?\*\*(.*?)\*\*.*?
=> $1
给了我:
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]);
});
关于javascript - 非贪婪地隔 ionic 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42559167/