Skip to content
Advertisement

Need Help extracing a list from a string using regex

I have a little trouble extracting these two lists ‘[‘item1’, ‘item2’, ‘item3’] [‘item4’, ‘item5′]’ here is an example of the code I have attempted

JavaScript

The output I received was

JavaScript

I would like to know how I can go about extracting these two lists from this long string specifically [‘item1’, ‘item2’, ‘item3’] and [‘item4’, ‘item5’]

Advertisement

Answer

JavaScript

Result:

JavaScript

Explanation:

  • [ matches the first bracket
  • [^]] matches anything that isnt a closed bracket
  • [^]]*? matches a variable number of non-bracket chars until the comma
  • (?: starts a non-capturing group (used for findall)

The rest should be self-explanatory.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement