How to filter everything after a word using regex

This article was published on May 08, 2021, and takes less than a minute to read.

Given the follow string:


I wanted to remove EVERYTHING right after the world esbuild.

I could go with replace('/lib/main.js', '') but this is not future proof. What if in the future it changes to /lib/index.js?

So I came up with a regex to do that:



  • (?<=esbuild) - is a positive lookbehind. In other words, it needs to contains this group (esbuild) before the match I want to;
  • .* - every single character;
  • $ - assert possition at the end of a line