Skip to content
Advertisement

How to ensure that at least one of A and B sign appears in the regex?

JavaScript

I want to search the idx of sub-string that has , and at the same time, there must be digits(sign A) before or [初末底前](sign B) behind “年”, for example,in s_l, it should return 4,5,0

One idea to divide the regex, like

JavaScript

but it is too complex, other one is using (?=...) but I haven’t got the idea and how to use it,any suggestions?

Advertisement

Answer

You can use a lookbehind assertion to match an occurrence of that’s preceded by a digit. Use an alternation pattern to match one that’s followed by [初末底前]:

JavaScript

This outputs:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement