const regex = /^(?<![\s\S\r])(.*)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<![\\s\\S\\r])(.*)', 'gm')
const str = `# project1
I could have put any other inline tag or string there.
This main body line could have been the very first line as well, if no tags exist.
Change the keys after each \`\\n\` and add or remove any more keys you want.
In this example, I added the desired tag in the indented multi-line format favoured by Obsidian.
As this is not a Templater solution, \`date modified\` is hard-coded here for the date you are making your changes. If not needed, it can be removed for the substitution part.
If it is a first time doing regex changes, make a backup of vault or Diary folder.`;
const subst = `---\naliases: \ntopic: \ntags: \n - m/diary\npublish: false\ndate modified: 2023-09-21\n---\n\n$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions