简体   繁体   中英

Match pattern with and without spaces

I am using markjs to search a page and highlight things that would be considered dice rolls. So it using the regex expression /([1-9]\\d*)?d(?:4|6|8|10|12|20)([+-]\\d+)?/i which actually works pretty well when the text is formatted as so 1d10+2 however if the text has any spaces, it only grabs highlights the first part. For example, if the same thing is written as 1d10 + 2 it will only highlight the 1d10 .

Is there something I can adjust in my regex that will account for spaces around the + or - symbols?

您可以将\\s用作空格,因此您的正则表达式可能如下所示:

([1-9]\d*)?d(?:4|6|8|10|12|20)\s?([+-]\s?\d+)

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