Richard said:
Mark Szlazak wrote:
Following your lead John, one maybe able to check for balance by
replace all non () characters with blanks and then seeing if the
remaining string length is even or odd.
')))('.replace(/[^()]/, '').length ; //even
- but:-
a = testString.replace(/[^(]/g, '');
b = testString.replace(/[^)]/g, '');
if(a.length != b.lenght){
// Parenthesise cannot match.
}else{
// There are the same numbers of each type of parenthesise.
// but that does not mean that nesting or sequence of
// parenthesise are "correct". var testString = '))((';
}
[THANK YOU for pointing that out!!!]
no matter how you slice it -- this is a two pass test... one pass *must*
remove all legitimately balanced parens:
var hold = origStr;
/*this is destructive -- so in a real setting, origStr needs 2 copies:
copy and hold*/
while(hold !=(origStr = origStr.replace(/(\([^\(\)]*\))/g,"")))
hold = origStr;
then test the remainder for orphan parens:
var left = (origStr.match(/\(/g) || []).length;
var right = (origStr.match(/\)/g) || []).length;
/* match returns 'null' if no matches -- so || with empty array for
length 0*/
var balance = left - right;
if balance is zero -- the string actually matches
less than zero -- stray left parens
greater than zero -- stray right parens
in the case of )( -- left and right are 1 but balance is zero, so the
test of "true" balance becomes:
var truebalance = !balance && !left && !right; (or any variation on this
test...like: !(balance | left | right) ... it has to be bitwise OR
because +/- anding cancel)
the var balance can be used to locate the first unbalanced paren --
if < 0 then iterate through an unaltered copy of the original string
using indexOf("(", startindex) and if > 0, iterate using lastIndexOf().
here's my offering:
http://fxmahoney.com/demo/balancedParensObj.htm --
it seemed logical to prototype the method to the String object...it
returns an array of information about the string: balanced, index into
the string of first "offending" paren (or -1), left/right context if
unbalanced, a "display" string (red text shows "error range"), and the
results of the successive reduction regex, i.e., the leftovers (could be
useful...) [as I write, I'm thinking it should return the
leftContext/rightContext from RegExp which could be used to more quickly
isolate the offending parens...i'll work on it later]
source code on page is filtered through a php colorizing script -- view
source for the actual code (in case of discrepencies.)
the default string is unbalanced -- also try out:
this is an )absurd( use of parens... plus any other combination you can
think of... i don't claim this an authoritative solution by any stretch,
but so far, it seems to work in every case (i've tried)...
fox