balanced-match
Advanced tools
Comparing version
module.exports = balanced; | ||
function balanced(a, b, str) { | ||
if (a instanceof RegExp) a = maybeMatch(a, str); | ||
if (b instanceof RegExp) b = maybeMatch(b, str); | ||
var r = range(a, b, str); | ||
@@ -14,2 +17,7 @@ | ||
function maybeMatch(reg, str) { | ||
var m = str.match(reg); | ||
return m ? m[0] : null; | ||
} | ||
balanced.range = range; | ||
@@ -16,0 +24,0 @@ function range(a, b, str) { |
{ | ||
"name": "balanced-match", | ||
"description": "Match balanced character pairs, like \"{\" and \"}\"", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"repository": { | ||
@@ -16,3 +16,3 @@ "type": "git", | ||
"devDependencies": { | ||
"tape": "~4.2.2" | ||
"tape": "~4.5.0" | ||
}, | ||
@@ -19,0 +19,0 @@ "keywords": [ |
# balanced-match | ||
Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. | ||
Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regular expressions as well! | ||
@@ -19,2 +19,3 @@ [](http://travis-ci.org/juliangruber/balanced-match) | ||
console.log(balanced('{', '}', 'pre{first}between{second}post')); | ||
console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post')); | ||
``` | ||
@@ -32,2 +33,3 @@ | ||
post: 'between{second}post' } | ||
{ start: 3, end: 17, pre: 'pre', body: 'in{nest}', post: 'post' } | ||
``` | ||
@@ -34,0 +36,0 @@ |
@@ -83,3 +83,11 @@ var test = require('tape'); | ||
}); | ||
t.notOk(balanced(/\{/, /\}/, 'nope'), 'should be notOk'); | ||
t.deepEqual(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'), { | ||
start: 3, | ||
end: 17, | ||
pre: 'pre', | ||
body: 'in{nest}', | ||
post: 'post' | ||
}); | ||
t.end(); | ||
}); |
8851
7.28%142
10.94%92
2.22%