-1

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.

Akshin Jalilov
  • 1,658
  • 1
  • 12
  • 12
  • 1
    brackets mean "capture group". You need to escape them. – MightyPork Aug 05 '13 at 16:17
  • Try `'hi (hi)'.match('hi \\(hi\\)')`. – Denys Séguret Aug 05 '13 at 16:17
  • Its a good idea to add a `;` after each variable declaration or a `,` incase you are declaring them together . I see them missing in the above. – woofmeow Aug 05 '13 at 16:20
  • Yes sorry, I have built this small example in a bit if a hurry. Original has semicolons. I will try to get rid of the brackets when assigning the data in to an array. – Akshin Jalilov Aug 05 '13 at 16:32
  • Did you know that `match` works with regular expressions? If you don't want that, use equality (`m==y`) or [`contains`](http://stackoverflow.com/questions/1789945/method-like-string-contains-in-javascript) – Bergi Aug 05 '13 at 16:33
  • Yes, the thing is that the variable 'm' will have a second part so equality won't work. I might end up braking it to two parts and compare just the first part if I don't find a better way. – Akshin Jalilov Aug 05 '13 at 16:37
  • @AkshinJalilov: What do you mean by "second part", could show that in your post please? – Bergi Aug 05 '13 at 16:40
  • By the way, you could use this as a "catch-all" function to escape all possible characters: http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript in case it's dynamic and the text could technically contain any special regex characters – Ian Aug 05 '13 at 16:41
  • I have edited the post to show the second part – Akshin Jalilov Aug 05 '13 at 16:43
  • @Ian: I have edited the post. Is this what you mean, or am I missing something else? '/[-\/\\^$*+?.()|[\]{}]/g' this shows as a red line, which probably means that script is not accepting it. – Akshin Jalilov Aug 05 '13 at 17:02
  • @AkshinJalilov You're escaping the wrong thing: http://jsfiddle.net/7CKNT/ . You need to escape the string being treated as a regex, not the original string to match against – Ian Aug 05 '13 at 17:25
  • Seriously, if you don't need regex don't use regex. Use `indexOf` or a [`contains` function](http://stackoverflow.com/questions/1789945/method-like-string-contains-in-javascript) instead of `match`! – Bergi Aug 05 '13 at 17:37
  • @Bergi: indexOf() breaks the script, once it reaches the value with a bracket, where match only ignores it. Contains function gave an error saying it is not present in the object. My data is imported as a parameter from another script so it comes in the form of an array, I then use different parts of this array in the for loop to compare (i.e. value[i]). Tried using value[i].parameter.contains, gave an error stating that parameter is undefined. – Akshin Jalilov Aug 05 '13 at 17:56
  • @Akshin: How does it "*break*" it? Sounds like you're not using it correctly. The question about `contains` has all necessary information (a. o. the fact that it's not natively available so you have to add it) – Bergi Aug 05 '13 at 19:06

2 Answers2

0

Because parenthesis have a special meaning in a regular expression, they must be escaped to be seen as literals.

As dystroy comment, you must write:

'hi \\(hi\\)'
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

You have to replace the content of the variable y, you have to escape parentheses:

http://jsfiddle.net/xafUT/

var m = 'hi (hi)'
var n = 'hi'
var y = 'hi \\(hi\\)'
var z = 'hi'

if (m.match(y)) {
   alert('yes');
} else {
    alert('no');
}
if (n.match(z)) {
    alert('yes');
} else {
    alert('no');
}
amatellanes
  • 3,645
  • 2
  • 17
  • 19