const regex = /\b(t)(?:anks *(?=[.?!]\n|to|for|in|ever)|[han]{3}([ks]{2}|x)+|hx|anx)\b/gi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\b(t)(?:anks *(?=[.?!]\\n|to|for|in|ever)|[han]{3}([ks]{2}|x)+|hx|anx)\\b', 'gi')
const str = `Fix:
thx 28,227 results
thanx 12,147
Tanks 1,087 <-- may actually mean _tanks_.
tanks in advance
tanks everyone
tanks for the help
Tanks!
thansk 518
tanx 125
tahnks 97
Leave alone:
thank you
tank, tanks of acid,
task, tasks
Suppose there is a production line with 8 tanks: each filled with a different substance for parts to be dipped in. The parts will be dropped into tanks by a crane along side the tanks. (http://stackoverflow.com/questions/25062870/)`;
const subst = `$1hanks`;
// 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