const regex = /((volume|part)|(^,)\s?(vol|pt)[^.]+?)/gism;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((volume|part)|(^,)\\s?(vol|pt)[^.]+?)', 'gism')
const str = `Should Match:
a) Any occurences of "Volume" OR "Part" (Case Insensitive);
b) Any occurence of "vol" or "pt" (CI) that does not have [[comma][space] before AND [period] after;
My volume 1
My name vol2.
My, volume 1
, pt. vol2.
, part.
, pt.
, pt
, vol
. pt.
// Should match below as there is no [period] after it
The Red Pill, Pt 2
, Pt 2`;
const subst = `\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