First, the dynamic part must be escaped, else, .
will match any char but a line break char, and will match ab@xyz§com;
, too.
Next, you need to match this only at the start of the string or after ;
. So, you may use
var Full_str1 = 'ab@xyz.com;cab@xyz.com;c-ab@xyz.com;c.ab@xyz.com;c_ab@xyz.com;';
var removable_str2 = 'ab@xyz.com;';
var rx = new RegExp("(^|;)" + removable_str2.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "g");
console.log(Full_str1.replace(rx, "$1"));
// => cab@xyz.com;c-ab@xyz.com;c.ab@xyz.com;c_ab@xyz.com;
Replace "g"
with "gi"
for case insensitive matching.
See the regex demo. Note that (^|;)
matches and captures into Group 1 start of string location (empty string) or ;
and $1
in the replacement pattern restores this char in the result.
NOTE: If the pattern is known beforehand and you only want to handle ab@xyz.com;
pattern, use a regex literal without escaping, Full_str1.replace(/(^|;)ab@xyz\.com;/g, "$1")
.