juliangruber / balanced-match

Match balanced character pairs, like { and }
MIT License
125 stars 40 forks source link

Regex in arguments #4

Closed whaaaley closed 8 years ago

whaaaley commented 9 years ago

Useful when you have a repeating or an unknown amount of characters in a or b and don't want them in the result.

Example

string = 'call   (   10   )   ';

Solution

console.log(balance(/\s+\(\s+/, /\s+\)\s+/, string);

Returns

{ start: 4, end: 13, pre: 'call', body: '10', post: '' }
juliangruber commented 9 years ago

..otherwise LGTM!

neytema commented 8 years ago

Failing test:

test('regexp', function(t) {
  t.deepEqual(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre  {   in{nes }   }  post'), {
    start: 3,
    end: 17,
    pre: 'pre',
    body: 'in{nes }',
    post: 'post'
  });
});