newline之间的字符串之间的字符串正则态度是什么?



用newline捕获字符串inbetweeen定界器的正则是什么。

字符串是:

/* start */This is a test of regex

使用新线条和(特殊char(

asdsadasd/* end*/

请提出一条发出的正则输出: - ***这是对正则使用新线条和(特殊char(

asdsadasd

即/* start */and/* end */。

之间的字符串
Here **/* start */** and **/* end */** are the **delimiters**.

此正则表达式将匹配您的追随者: //* start */(.*?)/* end *//gs

  • *需要逃脱,因为它们是Regex中的特殊字符
  • (.*?)部分告诉其捕获包含任何字符的最短匹配
  • 结尾处的gs告诉它支持多个匹配(g(,并且.应匹配新的line字符(s(

示例代码:

const regex = //* start */(.*?)/* end *//gs;
const str = `/* start */This is a test of regex
with new line and (special char)
asdsadasd/* end */
assorted unmatched crap
/* start */another match/* end */
blah blah blah
/* start */another
multi
line
match
/* end */
`;
let match;
while ((match = regex.exec(str)) !== null) {
    console.log(`Found a match: ${match[0]}`);
    console.log('----------------------------------');
}

您可以使用[^]*?

示例:

var string = `/* start */This is a test of regex
with new line and (special char)
asdsadasd/* end */`;
var result = string.match(//* start */([^]*?)/* end *//);
console.log(result[1]);

最新更新