const regex = /[^[:graph:]*](?#Do not match on any visible character)
\K\*(?#Reset starting point and then match on a *)
([[:alpha:].,\ ]+(?:\*{2}[[:alpha:].,\ ]+\*{2}?\ ?)?)(*COMMIT)
\*\ ?(?#Match on ending * then stop matching with COMMIT)/mg;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('[^[:graph:]*](?#Do not match on any visible character)
\\K\\*(?#Reset starting point and then match on a *)
([[:alpha:].,\\ ]+(?:\\*{2}[[:alpha:].,\\ ]+\\*{2}?\\ ?)?)(*COMMIT)
\\*\\ ?(?#Match on ending * then stop matching with COMMIT)', 'mg')
const str = `This text is not italic.
*This text is italic.*
This text is *partially* italic
This text has *two* *italic* bits
**bold text (not italic)**
**bold text with *italic* **
**part bold,** *part italic*
*italic text **with bold** *
*italic* **bold** *italic* **bold**
*invalid markdown (do not parse)**
random * asterisk`;
const subst = `<i>$1</i> `;
// 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