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:
- Regex To Match The First Word Of Each Line In A Multiline Text Block
- Regex To Match The First Word After A Specific Word
- Regex To Match Words With No Digits