const regex = /(class=(\\?(?:"|'){1,2})(?:[-a-z0-9 ]* )?)success(?=\2|\s)/gi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(class=(\\\\?(?:"|\'){1,2})(?:[-a-z0-9 ]* )?)success(?=\\2|\\s)', 'gi')
const str = `// Match these
class='something success'
class="success"
class='something success another"
class="something-else success test"
class=\\'success\\'
class=\\"success\\"
class=\\"modal success text-success\\"
class=' success'
CssClass="old-asp-way success"
response.WriteLine(" <div class=""success""></div>")
response.WriteLine(' <div class=''whatever success something''></div>')
// Don't match these
class="text-success"
class="success-something"`;
const subst = `$1changed`;
// 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