...

View Full Version : Advanced String Searching



canobi2006
01-24-2006, 07:04 PM
Hi everyone,

I'm trying to create a script that will search for string occurrences. However, I want do it in such a way that I do not necessarily know what string I'm looking for as long as it is within two defined strings.

For example, say I have the following string
--------------------------------------------
'name="firstname" name="lastname" age="22" name="middlename"'

I want to be able to get all the strings between name="" i.e. firstname, lastname, and middlename into an array.

Can anyone show me how I can script this? All I need is a guide of the process as I can do the coding. Better still if there's some readymade code, that would help as well.

Thanks.

cyber11
01-24-2006, 09:52 PM
I have one solution in Perl that is amazingly simple but my translation to JS is such that I can't get it to work in a similar manner.

$string = 'name="firstname" name="lastname" age="22" name="middlename"';
@anams=($string =~ m/name\s*=\s*\"([^\"]+)\"/ig);
print qq~@anams\n~;

I also have a solution in JS that works despite it being ugly, it gets the job done.

<script type="text/javascript">

myText = 'name="firstname" name="lastname" age="22" name="middlename"';
var my_array = myText.split(" ");
var a = new Array(my_array.length)

for (var i=0; i < my_array.length; i++){
thisword =my_array[i];
if(thisword.match(/name\s*=\s*\"([^\"]+)\"/ig)){
finalresult = thisword.replace(/name\s*=\s*\"([^\"]+)\"/ig,"$1");
a[i]=finalresult+"\n";
}
}
s=a.join("");

alert(s)
</script>

-Bill

shyam
01-25-2006, 08:11 AM
here's how i'd do it :)



var text = 'name="firstname" name="lastname" age="22" name="middlename"';
var m;
var matches = [];
do {
m = /name\s*=\s*"([^"]+)"/.exec(text);
if ( m ) {
text = text.substring(m.index+m[0].length);
if ( m[1] ) {
matches[matches.length] = m[1];
}
}
} while ( m );
for ( var i = 0; i < matches.length; i++ ) {
alert("matches[" + i + "] = '" + matches[i] + "'");
}

konithomimo
01-25-2006, 08:09 PM
I would do it like this:

function getNames(mytext)
{
var checkText = mytext.toString();
var firstSplit = checkText.split(' ');
var secondSplit = new Array();
var nameArray = new Array();
var i;
var j = 0;

for(i=0;i<firstSplit.length;i++)
{
secondSplit[i] = firstSplit[i].split('=');
if(secondSplit[i][0] == 'name')
{
nameArray[j] = secondSplit[i][1];
j++;
}
}
alert(nameArray);
}

Then just send it the text that you want to edit.

getNames('name=firstname name=lastname age=22 name=middlename')

Or remove the argument from the function and just hard code in a value for the text:


function getNames()
{
var mytext = 'name=firstname name=lastname age=22 name=middlename';
var firstSplit = mytext.split(' ');
var secondSplit = new Array();
var nameArray = new Array();
var i;
var j = 0;

for(i=0;i<firstSplit.length;i++)
{
secondSplit[i] = firstSplit[i].split('=');
if(secondSplit[i][0] == 'name')
{
nameArray[j] = secondSplit[i][1];
j++;
}
}
alert(nameArray);
}

Then just call it like this:

getNames()

Of course, you can keep in the double quotation marks, but I just took them out for testing.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum