Here is an example what I am trying to do:
function myFunction() {
var m = 'hi (hi) by akshin Jalilov';
var n = 'hi by akshin jalilov';
var y= 'hi (hi)';
var z= 'hi';
if (m.match(y)) {Logger.log('yes');
}else {Logger.log('no');}
if (n.match(z)) {Logger.log('yes');
}else {Logger.log('no');}
}
In the first case the result is 'no', while the second one shows 'yes'. Why the presence of the brackets in the string breaks the '.match'? Is there a way to avoid this, if the data I input has to have brackets?
Thanks in advance for the help.
Thanks a lot to Ian for the solution.
RegExp.escape = function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
function myFunction() {
var m = 'hi (hi) by akshin Jalilov';
var n = 'hi by akshin jalilov';
var y = 'hi (hi)';
var z = 'hi';
if (m.match(RegExp.escape(y))) {
console.log('yes');
} else {
console.log('no');
}
}
Thanks to everyone for the help.