1

When pattern is being tested in https://regex101.com/r/YbRw2h/1, it is displaying two matches.

var patt = /\{panel:bgColor=#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})}(\r\n|\r|\n)?(.*?){panel}/gm

var str = `{panel:bgColor=#deebff}\nThis is info panel\n{panel}fkjfkfwkwfj\
    
                 {panel:bgColor=#deebff}\nThis is info panel\n{panel}`


console.log(patt.test(str)) //false

How to fix regex?

4
  • It should be var str = String.raw`{panel:bg etc... Commented Jul 29, 2021 at 13:45
  • @Thefourthbird Why? Commented Jul 29, 2021 at 14:10
  • @mplungjan If you want to display the 2 matches, you get this regex101.com/r/BNMqUq/1 But I think that the OP wants to match a newline instead of \n literally. Something like {panel:bgColor=#(?:[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})}[^]*?{panel} perhaps regex101.com/r/TFz7Ga/1 Commented Jul 29, 2021 at 14:17
  • 1
    @Mahi Did that work out? Commented Aug 2, 2021 at 7:31

1 Answer 1

1

You could use:

{panel:bgColor=#(?:[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})}[^]*?{panel}

Explanation

  • {panel:bgColor=# Match literally
  • (?: Non capture group for the alternation
    • [A-Fa-f0-9]{6} Match 6 occurrences of A-F a-f or a digit 0-9
    • | Or
    • [A-Fa-f0-9]{3} Match 3 occurrences of A-F a-f or a digit 0-9
  • ) Close non capture group
  • } Match literally
  • [^]*? Match any char including newlines non greedy
  • {panel} Match literally

Regex demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.