How to find-replace leading newlines in Cursor

I had a bunch of files with empty newlines at the start of the documents. I tried searching for these in Cursor (this should also work for VSCode) using \A to match at the absolute beginning of the file, but VSCode considered that invalid syntax. So I had to use negative-look-behind instead:

(?<![\s\S])(?:\r?\n)+

Similarly, to delete one or more empty lines at the end of files, you can use:

(\r?\n)(?:\r?\n)+$

...where $ means the very end of the file.

Copyright Ricardo Decal. richarddecal.com