balanced string matching, and replacing.
install
npm install node-balanced
example time
lets say you have
} @ } @ }}
and you would like to replace the @hello block easily, balanced allows you to do this
var balanced = ; balanced;
this is a simple and efficient way to make balanced replacements, without a parser.
matching
you can get balanced matches by doing the following
var balanced = ; balanced;
multiple head/open/close
you can match multiple head/open/close efficiently by doing this
var isBalanced = balanced;
ignore
ignore is supported by the matches
and replacements
methods, this is very useful for something like not matching inside of comments
var blockComments = balanced.matches({source: source, open: '/*', close: '*/'}),
singleLineComments = balanced.getRangesForMatch(source, /^\s*\/\/.+$/gim);
balanced.matches({
source: source,
head: /@hello \d \{/,
open: '{',
close: '}',
ignore: Array.prototype.concat.call([], blockComments, singleLineComments),
replace: function (source, head, tail) {
return head + source + tail;
}
});
advanced
in this example we have code and we want to avoid replacing text thats inside of the multiline/singleline comments, and quotes
}/* @hello 2 { a { } }*/ @ }// @} @";
with balanced you can do this
// returns quote ranges with option ignore filter { var quotes = balanced; // filter out ingored ranges if ignore quotes = balanced; var currect = null ranges = ; quotes; return ranges; } var blockComments = balanced singleLineComments = balanced ignores = Arrayprototypeconcat quotes = ; // remove ignores inside of quotes ignores = balanced; // optional ignore code inside of quotes ignores = ignores; // run your matches or replacements method balanced;
as you can see by using these principles you can accomplish this kind of stuff easily