0

I my regex doesnt work if there is multiple lines.I'm trying to match that everything inside /#----- #/ symbols. In one single line it works fine. You can check it here: 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

 #/ 
Yerlan Yeszhanov
  • 2,149
  • 12
  • 37
  • 67

2 Answers2

0

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>`,
    )
  }
欧阳维杰
  • 1,608
  • 1
  • 14
  • 22
0

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

Rafael
  • 7,605
  • 13
  • 31
  • 46