0

I use javascript. I need to match a string that would be like this (if I had to describe it with real words):

(NOTHING before) + (only ONE word, not optional) + (".couleur" (it is a "fixed" word)) + (only ONE OPTIONAL SPACE) + ("=" (fixed word)) + (only ONE OPTIONAL SPACE) + (either the word "vert" or "violet" or "orange") + (NOTHING after)

I tried this :

   var re1='((?:[a-z][a-z]+))'; 
   var re2='(\\.)'; 
   var re3='(couleur)'; 
   var re4='(\\s?)';    
   var re5='(=)';   
   var re6='(\\s?)';    
   var re7='((?:[a-z][a-z]+))'; 

   var p = new RegExp(re1+re2+re3+re4+re5+re6+re7,["i"]);

But I don't find how to avoid matching when there is something BEFORE and something AFTER my string, and I don't know how to check if the last part is "vert" "orange" or "violet" to match it.

EDIT: here are some string that would match :

"test.couleur = vert" 
"te.couleur = violet"

and here are some string that wouldn't match:

" test.couleur = vert" (space before)
" e tdk.couleur = vert" (something before the first word)
"tdl.couleur = vert e" (something after the last word)
"tdk.couleur = gris" (gris is not "vert" or "orange" or "violet")

Thanks a lot for your help!

Theo Babilon
  • 661
  • 6
  • 18

2 Answers2

1

This regular expression should fit your needs:

var re = new RegExp('^\\w+\\.couleur\\s?=\\s?(vert|violet|orange)$', 'i');

You can test it here.

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • There is some serious issue with the solution in this answer (`new RegExp('^\w+\.couleur\s?=\s?(vert|violet|orange)$', 'i')`). [Backslashes](http://stackoverflow.com/questions/10769964/backslashes-regular-expression-javascript). – Wiktor Stribiżew Apr 21 '16 at 13:52
  • @WiktorStribiżew: you're right... I did just corrected it... When answer was accepted it was working because I did not use RegExp, but inline pattern /.../. I changet it to better reflect OP question, using RegExp... – MarcoS Apr 21 '16 at 14:02
  • forgot to mention that i used http://stackoverflow.com/questions/6521572/javascript-regex-not-working this answer to make it work. But that was a good regexp anyway @WiktorStribiżew – Theo Babilon Apr 21 '16 at 14:10
  • Good. BTW, "should-fit-your-needs" is a type of explanation I do not understand, but most SO users are happy with it. No idea why. The other answer is identical, anyway :) Good day. – Wiktor Stribiżew Apr 21 '16 at 14:12
  • ??? It's not an "explanation", it's an "introduction"... @timolawl answer is similar, but *later*... :-) – MarcoS Apr 21 '16 at 14:15
  • @MarcoS how can I change your regexp to change "w+" at the beggining with a js variable ? Can't make it work actually... – Theo Babilon Apr 21 '16 at 14:51
  • `var re = new RegExp('^' + jsVariable + '\\.couleur\\s?=\\s?(vert|violet|orange)$', 'i');` – MarcoS Apr 21 '16 at 14:55
  • @MarcoS Yes, and I learned a valuable lesson from this. I already had my solution but I decided to write out a fully functional example before submitting it. If I had simply submitted the one liner first, it's arguable who would have been first :). – timolawl Apr 22 '16 at 02:55
1

This should work:

var regex = /^\w+\.couleur\s?=\s?(vert|violet|orange)$/i;

Here it is in action with all your test strings:

var strArr = ["test.couleur = vert", "te.couleur = violet", " test.couleur = vert", "  e tdk.couleur = vert", "tdl.couleur = vert e", "tdk.couleur = gris"];

var regex = /^\w+\.couleur\s?=\s?(vert|violet|orange)$/i;

strArr.forEach(function(str) {
  document.body.insertAdjacentHTML('afterend', str + ": " + regex.test(str));
});
timolawl
  • 5,434
  • 13
  • 29