Match any string between two different tokens

后端 未结 2 1927
情歌与酒
情歌与酒 2021-01-23 19:05

I\'m trying to match a part of a string between two different tokens. They might be multiple occurrences of the tokens in a string.

Sample text (tokens are italic, text

2条回答
  •  星月不相逢
    2021-01-23 19:36

    /x(.*?)y/g where x is the beginning token and y the ending token.

    This RegEx means: match anything (.), any number of times (*), as few times as possible (?).

    A direct example from your question would be:

    /\[begin-match\](.*?)\[end-match\]/g
    

    The sample text is now in the first capturing group.

提交回复
热议问题