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...

1
  • Where do name, class and somevalue come from? Do you only want to allow equal signs and square brackets around them? Commented Apr 29, 2013 at 21:19

3 Answers 3

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')

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

1 Comment

+1. Or maybe new RegExp(values.split(' | ').join('|')).test(str).
0

Is it what you are looking for ?:

/^(?:a|activ|blue|lagoon)\b(\[(?:name|class)(?:=somevalue)?])?$/.test(yourstring);

Comments

0
^(a|activ|blue|lagoon)(?:\[(\w+)=?(\w*)\])?$

For "activ[class=something]"

group 1 = activ
group 2 = class
group 3 = something

Comments

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.