Extract nested brackets from strings
npm install extract-brackets#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:
``javascript
`
'outer( inner )'
`
will return
javascript
`
' inner '
`
And parsing:
javascript
`
'outer( inner( innest ) )'
`
will return:
javascript
`
' inner( innest ) ' and ' innest '
`
It also works with escaped characters and characters that exist inside strings:
Parsing:
javascript
`
'outer( "inner( innest )" )'
`
will return
javascript
`
' "inner( innest )" '
`
and parsing:
javascript
`
'outer( inner\( innest ) )'
`
will return
javascript
`
' "inner\( innest )" '
`
To use:
javascript
`
var Extractor = require('extract-brackets')
open
You can define the str, the close str, and the string escape strs
close
If a char is not provided, it will default to the matching pair from the a list, or it will be the open string in reverse.
string escape
If a list is not provided, it will default to a common list.
str
Returns an array of match objects
A match object has three properties:
* - 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:
javascript
`
var ExtractParens = new Extractor( '(' );
//the 'close' char defaults to ')'
ExtractParents.extract('outer( inner( innest ) )')
`
will return:
javascript
``
[{
hasNest : true,
str : ' inner( innest ) '
nest : [
' inner',
{
str : ' innest ',
hasNest : false,
nest : [' innest ']
},
' '
]
}