Hamster
Hamster

Reputation: 577

Javascript, Regex - I need to grab each section of a string contained in brackets

Here's what I need in what I guess must be the right order:

  1. The contents of each section of the string contained in square brackets (which each must follow after the rest of the original string) need to be extracted out and stored, and the original string returned without them.
  2. If there is a recognized string followed by a colon at the start of a given extracted section, then I need that identified and removed.
  3. For what's left (comma delimited), I need it dumped into an array.
  4. Do not attempt to parse nested brackets.

What is a good way to do this?

Edit: Here's an example of a string:

hi, i'm a string [this: is, how] [it: works, but, there] [might be bracket, parts, without, colons ] [[nested sections should be ignored?]]

Edit: Here's what might be the results:

After extraction: 'hi, i'm a string'

Array identified as 'this': ['is', 'how']

Array identified as 'it': ['works', 'but', 'there']

Array identified without a label: ['might by bracket', 'parts', 'without', 'colons']

Array identified without a label: []

Upvotes: 2

Views: 195

Answers (1)

Kobi
Kobi

Reputation: 138007

var results = [];
s = s.replace(/\[+(?:(\w+):)?(.*?)\]+/g,
      function(g0, g1, g2){
        results.push([g1, g2.split(',')]);
        return "";
      });

Gives the results:

>> results =
  [["this", [" is", " how"]],
   ["it", [" works", " but", " there"]],
   ["", ["might be bracket", " parts", " without", " colons "]],
   ["", ["nested sections should be ignored?"]]
  ]

>> s = "hi, i'm a string     "

Note it leaves spaces between tokens. Also, you can remove [[]] tokens in an earlier stage by calling s = s.replace(/\[\[.*?\]\]/g, ''); - this code captures them as a normal group.

Upvotes: 3

Related Questions