Regex To Match A Part Of A String And Ignore Everything After A Particular Text

Category: Strings

When using a regular expression to find some text in a string it’s often required to select everything up to but not including that particular string. For example, when scanning for HTML tags it’s often required to select everything up to and excluding the word end since this would include both tags and punctuations.

This is a useful regex pattern that matches a part of a string and ignores everything after a particular text.

/^(.*?)regex/

Matches:

  • This is regex pattern
  • This is pattern regex.com
  • This is pattern regexcom

Non-matches:

  • Anything that doesn’t contain ‘regex’.

See Also:

Rate This Regex
Sending
User Review
0 (0 votes)

Expression Flags

Flags Description
i Ignore case sensitive.
g Allows global search.
m Allows multiline search.