简体   繁体   English

正则表达式,匹配包括多行

[英]regex , match including multiple lines

I my regex doesnt work if there is multiple lines.I'm trying to match that everything inside /#----- #/ symbols. 如果有多行,我的正则表达式不起作用。我正在尝试匹配/#-----#/ symbols中的所有内容。 In one single line it works fine. 在一行中它工作正常。 You can check it here: https://regex101.com/r/yZTXwh/3 您可以在此处查看: https//regex101.com/r/yZTXwh/3

code: 码:

highlightMessage(message) {
    return message.replace(
      /\/#\s*(.*?)\s*#\//g,
      (_, g) => `<span class='highlited-message'>${g}</span>`,
    )
  }

text: 文本:

Hello
 /# my name
is
Mike
nice to meet you

 #/ 

You could use [^]* to match any thing include a newline 您可以使用[^] *匹配包含换行符的任何内容

please try this: 请试试这个:

function highlightMessage(message) {
    return message.replace(
      /\/#\s*([^]*)\s*#\//g,
      (_, g) => `<span class='highlited-message'>${g}</span>`,
    )
  }

It looks like you're trying to match the /#...#/ block's significant characters: 看起来你正试图匹配/#...#/块的重要字符:

/\\/#\\s*((?:.|\\n)*?)\\s*#\\//gm

 let s = `Hello /# my name is Mike nice to meet you #/ bar /# single-line #/ foo /# multi-line #/ baz `; let r = /\\/#\\s*((?:.|\\n)*?)\\s*#\\//gm; let a; while ((a = r.exec(s)) !== null) console.log(a[1]); 

Here's a regex101 这是一个regex101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM