简体   繁体   中英

Regex test() method returning false for a valid regex having match

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?

You probably meant to escape the opening curly bracket at the end. Moreover, you have two line breaks in your string, so you might want to repeat the segment (\\r\\n|\\r|\\n)?(.*?) . Try with the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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