Javascript 正则表达式匹配带换行符的星号之间的字符串
例如:Javascript 正则表达式匹配带换行符的星号之间的字符串,javascript,jquery,regex,Javascript,Jquery,Regex,例如: blah blah * Match this text Match this text Match this text Match this text Match this text * more text more text 如何从带换行符的星号中获取字符串?[\s\s]匹配任何空格和任何非空格字符。即任何字符,甚至换行符。(经过测试) 试试这个:/(\*)([^\0].+)*(\*)/
blah blah * Match this text Match this text
Match this text
Match this text
Match this text
*
more text more text
如何从带换行符的星号中获取字符串?
[\s\s]
匹配任何空格和任何非空格字符。即任何字符,甚至换行符。(经过测试)
试试这个:/(\*)([^\0].+)*(\*)/g
:
您可以在此处使用否定匹配。注意,对于这个示例,我转义了
\
文字换行符
var myString = "blah blah * Match this text Match this text\
Match this text\
Match this text\
Match this text\
*\
more text more text";
var result = myString.match(/\*([^*]*)\*/);
console.log(result[1]);
// => " Match this text Match this text Match this text Match this text Match this text "
看
如果不需要前导或尾随空格,可以使用以下命令使其不贪婪
var result = myString.match(/\*\s*([^*]*?)\s*\*/);
console.log(result[1]);
// => "Match this text Match this text Match this text Match this text Match this text"
这些答案对你们两人都有帮助 从控制台:
> "blah blah * Match this text Match this text\
Match this text\
Match this text\
Match this text\
*\
more text more text".match(/[*]([^*]*)[*]/)[1]
" Match this text Match this text Match this text Match this text Match this text "
谢谢,我会调查的。虽然我不想在最终输出中使用星号,但这是可行的。谢谢。如果你提到你的捕获组,星号将不会出现在输出中。不过我看到输出在索引1中。然而,我很好奇为什么我的输入返回3个匹配,最后一个是数字?请参阅JsFiddle:您正在使用
for in
循环对数组进行迭代,这也将对属性进行迭代。在本例中,您还访问了index
属性,该属性表示字符串中匹配项的从零开始的位置。另外,您在代码中调用变量matches
,但值得注意的是,实际上您只找到了一个匹配项。
var result = myString.match(/\*\s*([^*]*?)\s*\*/);
console.log(result[1]);
// => "Match this text Match this text Match this text Match this text Match this text"
> "blah blah * Match this text Match this text\
Match this text\
Match this text\
Match this text\
*\
more text more text".match(/[*]([^*]*)[*]/)[1]
" Match this text Match this text Match this text Match this text Match this text "