const regex = /`[^`]+`|(?:(?:[ ]{4}|[ ]{0,3}\t).+(?:[\r\n]?(?!\n\S)(?:[ ]+\n)*)+)+/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('`[^`]+`|(?:(?:[ ]{4}|[ ]{0,3}\\t).+(?:[\\r\\n]?(?!\\n\\S)(?:[ ]+\\n)*)+)+', 'g')
const str = `So, code below throws undefined if index is not existing in the array
Note: it's array of arrays
arr[index] = arr[index].concat(data);
Eventually I've fixed it with check
\`if (!array[index]) {
array[index]=[];
}\`
which solved the problem.
Question: is there any javascript trick which could solve this problem easier (less lines of code, without checks, whatever)?
Here is some more code,
and a little more
One line in between
Two lines.
Three lines.
We like to preserve code exactly how it is.`;
const subst = `_xCodexBlockxPlacexHolderx_`;
// 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