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, ""));
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, ""));
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, '') );