views:

61

answers:

1

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: []

+3  A: 
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.

Kobi
Ah I see. Thanks.
Hamster
Yes, you'll want to use a regex, like `.split(/\s*,\s*/)`. A string doesn't work the same way, and `/g` isn't needed in `split` (but doesn't hut either `:)` )
Kobi
Ah, /\s*,\s*/. I'm still not completely clear on what /g does exactly.
Hamster
`/g` is **global**. When you `match` without `/g`, it only finds the first match, which is undesirable in most cases. `split`, however, works as expected with or without it.
Kobi
While this works, it leaves you with a seemingly "unwieldly" result. Personally, I would prefer to separate the string into parts using something like `str.split(/\s+(?=\[+)/);`, then iterate over the results afterwards.
Andy E
@Andy - I agree, it can be written more clearly that way. With more edge cases, that will be the only option.
Kobi
@Kobi: +1 for a working solution anyway :-)
Andy E