-1

I have two strings like these

var temp = 'xx-y1 xx-y2 xx-y3';
var temp1 = 'zz-y1 zz-y2 zz-y3';

I wanna replace all the words started with "xx-" and "zz-" pattern and for this purpose I do this.

temp.replace(/\bxx-\S+/g, '');
temp.replace(/\bzz-\S+/g, '');

now my question is how can I have a single function and just call it?

I try to test this but it doesn't work!!!

func = function(str, pattern) {
  return str.replace(RegExp('\b' + pattern + '\S+', 'g'), '');
}
AKZ
  • 856
  • 2
  • 12
  • 30

1 Answers1

1

You need to escape \ when calling RegExp constructor.

function replace(where, what) {
    return where.replace(new RegExp('\\b' + what + '\\S+', 'g'), '');
}
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98