After some minor reformatting of data from a textarea, from a loop I look at it with a variety of regular expressions. When I find one with an index of zero I create an appropriate entry in a work table, remove the length of that construct from the beginning of the string and loop again. A variable "maxOffset" is initialized to the length of the data and, if a found construct has an index > zero, that index will replace maxOffset if it is less that that current value. If I fall through to the end of the loop I will know how long the garbage is, create an error message, and remove it from the string.
Code:
while (lines.length > 0) {
var strLength, maxOffset = lines.length;
...
if ((result = lines.match(/\|\|/))) { //Found double bar line
if (result.index == 0) { lastfound = "double bar";
strLength = result[0].length;
genWorkEntry("Bar|Style:Double");
lines = lines.substr(strLength);
continue; }
else
maxOffset = (result.index < maxOffset)? result.index : maxOffset; }
if ((result = lines.match(/:\|(\d)/))) { //Found Repeat Close and Special ending
if (result.index == 0) { lastfound = "repclose special";
strLength = result[0].length;
genWorkEntry("Bar|Style:MasterRepeatClose");
genWorkEntry("Ending|Endings:" + result[1]);
lines = lines.substr(strLength);
continue; }
else
maxOffset = (result.index < maxOffset)? result.index : maxOffset; }
...
errText += "Unable to process: " + lines.substr(0,maxOffset) + " length: " + maxOffset + " last: " + lastfound + "\n";
genWorkEntry("Text|Text:\"" + lines.substr(0,maxOffset)
+ "\"|Font:StaffBold|Pos:-8|Wide:Y|Justify:Left|Placement:BestFit|Color:3|Visibility:Default");
lines = lines.substr(maxOffset);
} //End main parsing loop
Each of these constructs ends with the same (colored) lines. Is there a way to eliminate the need for this? switch and case seems close to the structure, but it can't do regular expressions.
The website is abcnwc.htm and this code is on lines 541-819.
Not very clear, but I have the impression that you are looking for a way to write dynamically a regular expression. That is to be done using the new RegExp() native Object/Constructor
Code:
var expressions=["\|\|",":\|(\\d)"], i=0, e, reg;
while(e=expression[i++]){
reg=new RegExp(e);
if(result = lines.match(reg)){
//... blah blah
}
}
You may also use a double array (or an object) in order to keep a correspondence between a certain regular expression and a certain string, in order to do something later.
Thanks for the links, I've bookmarked both of them.
Three things can happen on any match statement:
Finds nothing. Try the next match.
Finds something but result.index is > 0. If this is less than maxOffset replace it with result.index then try next match.
Finds something and result.index == 0. Perform unique processing for the found match, remove the length of the found construct from the beginning of lines, and ignore the rest of the loop.
If there was not a 3., maxOffset contains the length of the garbage to be removed from the front of lines after creating an error message.
In your suggested code, I think I can take the value of i to a switch statement to do the unique processing--that should get me started.
I ran into a few problems trying to check out this code:
All carriage returns and/or line feeds have been replaced with a literal "\r\n" before entering this loop but the display in the alert was on two lines. Can I convert this to a regexp without this being reinterpreted?
It quits with "invalid range in character class" on the header (occurrence [1]).
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function genNwc() {
var expressions=[
"/\\\r\\\n ?w:(.*$)", //lyric /\\r\\n ?w:(.*$)/
"/\\\r\\\n([A-Za-z]): ?([^\\\]*)", //header /\\r\\n([A-Za-z]): ?([^\\]*)/
"\"(_?)([^\"]+)\"", //found text /"(_?)([^"]+)"/
"\|\|", //double bar /\|\|/
":\\|(\\d)", //Repeat Close and Special ending /:\|(\d)/
], i=0, e, reg;
for (i=0; i < expressions.length; i++) {
e=expressions[i];
alert("i= " + i + " e ->" + e + "<-");
reg=new RegExp(e);
alert("i= " + i + " expr->" + reg.source + "<-");
}
}
</script>
</head>
<body>
<form name="nwcform">
<table>
<tr>
<td><input type="button" id="bClick1" value="Submit" onclick="genNwc()"></td></tr>
</table>
</form>
</body>
</html>
Bookmarks