Reputation:
Im trying to delete all console.() from string or text and this code doesnt work, why?
var str = "console.log('test')";
var pattern = /console\..*\(.*\);/gm;
console.log(str.replace(pattern, ""));
http://plnkr.co/edit/gzFPopi1qdd6PYZz2urM?p=preview
Upvotes: 1
Views: 301
Reputation: 193301
It doesn't work because in your test string there is no ;
however regexp expects one. Just make it optional with ?
:
var pattern = /console\..*?\(.*?\);?/gm;
Also make sure the match is not greedy with .*?
.
Check the test demo below.
var str = "some string console.log('test'); and console.log(123) \
console.log('123', 12, 'asd'); \
test";
alert( str.replace(/console\..*?\(.*?\);?/gm, '') );
Upvotes: 3