Skip to content Skip to sidebar Skip to footer

Match Any String Between Two Different Tokens

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 to match is

Solution 1:

/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.


Solution 2:

\[begin-match\]((?:(?!\[end-match\]).)*)\[end-match\]

You can try this.See demo.

https://regex101.com/r/uE3cC4/23


Post a Comment for "Match Any String Between Two Different Tokens"