anotherJEK
01-01-2012, 05:05 AM
I am now working on a utility function that will gather the indexes for substring matches found in a string. There is an academic mystery I am
puzzling over
function getMatchIndex(a, b) // a: sting to search b: what to search for
{
var out = new Array();
if(a.lastIndexOf(b) > -1) // at least two matches, ****! there could be more
{
if( (a.substr(a.indexOf(b) + b.length, a.lastIndexOf(b))).length > b.length )
{
var stp = parseInt(a.lastIndexOf(b)) - (parseInt(a.indexOf(b)) + parseInt(b.length));
alert(stp)
alert( (a.substr(a.indexOf(b) + b.length, stp)).length+' : '+a.substr(a.indexOf(b) + b.length, stp));
}
else
{
out[out.length] = a.indexOf(b);
out[out.length] = a.lastIndexOf(b);
return out;
}
}
else if(a.indexOf(b) > - 1 && a.lastIndexOf(b) == -1)
{
out[out.length] = a.indexOf(b);
return out;
}
else if(a.indexOf(b) == -1)
{
out = 'no matches';
return out;
}
else
{
var splitStr = new Array();
splitStr = a.split(b);
/*
possibilities:
1: one match at the begining or end
-- leaves one significant item
2: one match somewhere after the beginning and before the end.
-- gives two matches.
*/
for(var i = 0; i < splitStr.length - 1; i++) // don't want the last segment length
{
out[out.length] = splitStr[i].length
}
}
}
getMatchIndex('xxxi5ixxxxxxxi5ixx', 'i5i');
in the following code snippet:
if( (a.substr(a.indexOf(b) + b.length, a.lastIndexOf(b))).length > b.length )
{
var stp = parseInt(a.lastIndexOf(b)) - (parseInt(a.indexOf(b)) + parseInt(b.length));
alert(stp)
alert( (a.substr(a.indexOf(b) + b.length, stp)).length+' : '+a.substr(a.indexOf(b) + b.length, stp));
I am trying to get the sub string between the end of the first match to the beginning of the last match.
The 'if' test passes, but I have to parseInt the values to get the proper
value for stp.
SO, the question is, what is the actual data type of an index value obtained from a string. (It would appear to be a string; having to use it an a math expression doesn't work without parseInt).
(I hope I haven't posted too much code)
Thank you in advance.
puzzling over
function getMatchIndex(a, b) // a: sting to search b: what to search for
{
var out = new Array();
if(a.lastIndexOf(b) > -1) // at least two matches, ****! there could be more
{
if( (a.substr(a.indexOf(b) + b.length, a.lastIndexOf(b))).length > b.length )
{
var stp = parseInt(a.lastIndexOf(b)) - (parseInt(a.indexOf(b)) + parseInt(b.length));
alert(stp)
alert( (a.substr(a.indexOf(b) + b.length, stp)).length+' : '+a.substr(a.indexOf(b) + b.length, stp));
}
else
{
out[out.length] = a.indexOf(b);
out[out.length] = a.lastIndexOf(b);
return out;
}
}
else if(a.indexOf(b) > - 1 && a.lastIndexOf(b) == -1)
{
out[out.length] = a.indexOf(b);
return out;
}
else if(a.indexOf(b) == -1)
{
out = 'no matches';
return out;
}
else
{
var splitStr = new Array();
splitStr = a.split(b);
/*
possibilities:
1: one match at the begining or end
-- leaves one significant item
2: one match somewhere after the beginning and before the end.
-- gives two matches.
*/
for(var i = 0; i < splitStr.length - 1; i++) // don't want the last segment length
{
out[out.length] = splitStr[i].length
}
}
}
getMatchIndex('xxxi5ixxxxxxxi5ixx', 'i5i');
in the following code snippet:
if( (a.substr(a.indexOf(b) + b.length, a.lastIndexOf(b))).length > b.length )
{
var stp = parseInt(a.lastIndexOf(b)) - (parseInt(a.indexOf(b)) + parseInt(b.length));
alert(stp)
alert( (a.substr(a.indexOf(b) + b.length, stp)).length+' : '+a.substr(a.indexOf(b) + b.length, stp));
I am trying to get the sub string between the end of the first match to the beginning of the last match.
The 'if' test passes, but I have to parseInt the values to get the proper
value for stp.
SO, the question is, what is the actual data type of an index value obtained from a string. (It would appear to be a string; having to use it an a math expression doesn't work without parseInt).
(I hope I haven't posted too much code)
Thank you in advance.