I am going to construct a message-passing system whose messages have the following structure:
message type;message content
(matches message type;
)
However, the user can set the message type, and (for the sake of loosely coupled systems) I want to allow them to use a ;
as part of the message type. To do this, I'll have the message constructor escape it with a \
:
tl\;dr;Too long; didn't read content
(matches tl\;dr;
)
How can I have a regex match all content up to the first ;
that's not \;
? In the example, that's the tl\;dr;
part only. Note that there can be an unescaped ;
within the message content.
I tried ^.*;
, but that matches all content up to a semicolon within the message (e.g. tl\;dr;Too long;
)
/.*?[^\\](?=;)/
You could also just use ;
instead of (?=;)
, but the latter prevents it from being part of the full match.
If you only want to match from the start of the string, use:
/^.*?[^\\](?=;)/