[Javascript] regexp - how to exclude a substring?
Shawn Milo
shawn.milo at gmail.com
Mon May 23 11:18:11 CDT 2005
Paul,
I believe I have something that fulfills the requirements, although it
seems to freeze
my browser when I let it go through too many iterations.
Take a look, and let me know what you think. I think there's a
fundemental flaw somewhere, but I think we can find it as a group.
Everyone who takes the time to look at it: Feedback please! I know
there's some good stuff and some boo-boos in there, but I need help in
telling the one from the other. ;o)
Shawn
<html>
<head>
<title>Paul's DIV Dilemma</title>
<script type="text/javascript">
function parseDivs(sourceTA, destTA){
var startPos = 0;
var endPos = 0;
var longString = '';
var tempString = '';
var loopBreak = 0;
destTA.value = '';
longString = sourceTA.value.replace(/\n/gim, '');
destTA.value = destTA.value + longString.length;
destTA.value = destTA.value + '\n-----------------------------\n';
while (longString.indexOf('<div') && loopBreak < 10){
loopBreak += 1;
startPos = longString.indexOf('<div');
endPos = longString.indexOf('</div');
tempString = longString.substr(startPos, endPos - startPos + 5);
//If there is an opening div in here,
//then we haven't found the non-nested
//div. Otherwise, then we have. '
if (tempString.indexOf('<div')){
destTA.value = destTA.value + 'non match\n' + tempString;
destTA.value = destTA.value +
'\n-----------------------------\n';
}else{
destTA.value = destTA.value + 'match \n' + tempString;
destTA.value = destTA.value +
'\n-----------------------------\n';
}
longString = longString.substr(endPos + 5);
}
destTA.value = destTA.value + '\n' + loopBreak + ' iterations.\n';
}
</script>
</head>
<body>
<form id="frmTest" method="post" action="pdd.html">
<textarea cols="50" rows="20" id="bigText"></textarea>
<input type="button" id="btnParse" value="Parse DIVs"
onclick="parseDivs(document.forms['frmTest'].bigText,
document.forms['frmTest'].txtOut);" />
<textarea cols="50" rows="20" id="txtOut"></textarea>
</form>
</body>
</html>
More information about the Javascript
mailing list