0

I need to be able to determine if a string matches a list of defined values.

Say I have a defined list of strings, e.g.

a | activ | blue | lagoon | ex (defind list is much longer and contains approx. 50+ possibilties)

I should be able to match any of the following;

 a
 a[name]
 activ
 activ[class=somevalue]
 blue
 lagoon[name=somevalue], ...e.t.c

is it possible via regex to determine if a string passed is contained in the defined list?

thanks...

ClaraU
  • 877
  • 3
  • 11
  • 22
  • Where do `name`, `class` and `somevalue` come from? Do you only want to allow equal signs and square brackets around them? – Bergi Apr 29 '13 at 21:19

3 Answers3

1

As long as the string is consistently stored as: value | value (with one space between the value and pipe) you could trivially do:

str.match(new RegExp(values.split(' | ').join('|'))

If you need to math exact words you could use values.split(' | ').join('\\b|\\b')

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0
^(a|activ|blue|lagoon)(?:\[(\w+)=?(\w*)\])?$

For "activ[class=something]"

group 1 = activ
group 2 = class
group 3 = something
LINEMAN78
  • 2,562
  • 16
  • 19
0

Is it what you are looking for ?:

/^(?:a|activ|blue|lagoon)\b(\[(?:name|class)(?:=somevalue)?])?$/.test(yourstring);
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125