1

I have to test a string like this one in javascript:

var str = "eleme_eus = \'Generikoa_Erlijioa\' or eleme_eus like \'Eliza_igles_a\' or eleme_eus like \'Parroki_a\' or eleme_eus like \'Erm_ita\' or eleme_eus like \'Komen_tua\' or eleme_eus like \'Santutegia\'";

I've been trying to create a regex to do so, but I cannot pass the \' part.

For now, I have (working):

var regex = /\w\s(=|like)\s/;

After that, I've tried scaping the backslash, the quotation mark... always false.

I just need to get to the "or/and" part, after that it repeats itself.

Thank you in advance.

EDIT:

With both websites (thx), I've come to this expression:

enter image description here

\w+[-\w]*\s(=|like)\s\\\'\w+\\\'(\s(and|or)\s\w+[-\w]*\s(=|like)\s\\\'\w+\\\')*

but if I test it in the console (Chrome)...

enter image description here

regex.test("field-name like \'word\' or field-name like \'word_word\'");
1
  • I suggest using tools like this to test our regex Commented Apr 10, 2017 at 14:54

2 Answers 2

3

Well here you need to escape both the / and the ', like this:

var regex = /\w\s(=|like)\s\\\'\w+\\\'/;

You can see it's correct in Regex101 Demo.

Sign up to request clarification or add additional context in comments.

3 Comments

I see it working in that site and I already tested that. But regext.test("...") returns always false :S
@SergioTx Can you please update you question with you whole code?
OK, added some more info.
2

This regex should work for you:

var re = /\w+[-\w]*\s(?:=|like)\s'\w+'(?:\s(?:and|or)\s\w+[-\w]*\s(?:=|like)\s'\w+')*/g;

RegEx Demo

var str = "field-name like 'word' or field-name like 'word_word'";
re.test(str);
//=> true

str = "eleme_eus = \'Generikoa_Erlijioa\' or eleme_eus like \'Eliza_igles_a\' or eleme_eus like \'Parroki_a\' or eleme_eus like \'Erm_ita\' or eleme_eus like \'Komen_tua\' or eleme_eus like \'Santutegia\'";
"eleme_eus = 'Generikoa_Erlijioa' or eleme_eus like 'Eliza_igles_a' or eleme_eus like 'Parroki_a' or eleme_eus like 'Erm_ita' or eleme_eus like 'Komen_tua' or eleme_eus like 'Santutegia'"    
re.test(str);
//=> true

Your input doesn't have literal backslashes because \' will be interpreted as just single '.

1 Comment

In the end, the problem was that I was testing with a string without escaped backslashes. My original string had the backslashes already escaped. I marked this as correct because my question is answered by this, but I wrote it wrong xDDD Too many hours, sorry guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.