extract-brackets

Extract nested brackets from strings

Usage no npm install needed!

<script type="module">
  import extractBrackets from 'https://cdn.skypack.dev/extract-brackets';
</script>

README

extract-brackets

To extract nested brackets from strings.

It will detect all brackets inside the string, and return the data from each bracket in a nested format.

For example:

Parsing:

'outer( inner )'

will return

' inner '

And parsing:

'outer( inner( innest ) )'

will return:

' inner( innest ) ' and ' innest '

It also works with escaped characters and characters that exist inside strings:

Parsing:

'outer( "inner( innest )" )'

will return

' "inner( innest )" '

and parsing:

'outer( inner\( innest ) )'

will return

' "inner\( innest )" '

To use:

var Extractor = require('extract-brackets')

You can define the open str, the close str, and the string escape strs

If a close char is not provided, it will default to the matching pair from the a list, or it will be the open string in reverse.

If a string escape list is not provided, it will default to a common list.

Returns an array of match objects

A match object has three properties:

  • str - The entire string inside the brackets
  • hasNest - If a nested bracket was found
  • nest - Array where each element is either a plain string or a nested match object

To extract parenthesis:

var ExtractParens = new Extractor( '(' );

//the 'close' char defaults to ')'

ExtractParents.extract('outer( inner( innest ) )')

will return:


[{
  hasNest : true,
  str : ' inner( innest ) '
  nest : [
    ' inner',
    {
      str : ' innest ',
      hasNest : false,
      nest : [' innest ']
    },
    ' '
  ]
}