norbre norbre - 1 year ago 159
Javascript Question

Regex - find all matches in a string with conditions (javascript)

Please help me, I am a regex beginner and I didn't find a soluton.

I have a string like this:


I would like to find all matches (name) with a "color" condition.
Example if:

  • color is RED, the result is: APPLE and BERRY

  • color is ORANGE, the result is: ORANGE

  • color is BLACK, the result is: BERRY

This is the way I tried:
This lists all of names (apple, orange, berry):


This find a match of two "condition":


But if I try to combine like this..


.. the result is 'apple' and 'orange",color:"orange"),(name:"berry",'

thank you

Answer Source

Here is a regex that finds all groups that contain the color, but the name must come before the color


for the string:


you'll get two matches and the name is in group #1 of each match

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download