Regular Expressions: How to get the effect of an AND THEN operator in compound expression?

前端 未结 3 1669
梦毁少年i
梦毁少年i 2021-01-23 09:11

I\'m struggling to work with regular expressions. I think I understand the individual expressions but combining something together has me completely stumped. I don\'t grasp th

3条回答
  •  感情败类
    2021-01-23 09:27

    You've almost got it. It's really as simple as replacing 'or' with | and replacing and with concatenation. Then make sure your groups are unmatching by adding ?: to the beginning of each:

    (?:<|<\/)(?:[1-9]|[1-4][0-9]|[5][0-7])>

    MDN has an explanation on the interaction of split and regex. But the short example-explanation is:

    'hi_joe'.split('_'); // ['hi', 'joe']
    'hi_joe'.split(/_/); // ['hi', 'joe']
    'hi_joe'.split(/(_)/); // ['hi', '_', 'joe']
    'hi_joe'.split(/(?:_)/); // ['hi', 'joe']
    

    Update per comment, if you'd like the <##> in your results array as well, wrap the regex in an additional set of parens.

    ((?:<|<\/)(?:[1-9]|[1-4][0-9]|[5][0-7])>)

提交回复
热议问题