Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2006, 06:04 PM   PM User | #1
canobi2006
New to the CF scene

 
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
canobi2006 is an unknown quantity at this point
Advanced String Searching

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.
canobi2006 is offline   Reply With Quote
Old 01-24-2006, 08:52 PM   PM User | #2
cyber11
New Coder

 
Join Date: Dec 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
cyber11 is an unknown quantity at this point
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.
Code:
$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.
Code:
<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

Last edited by cyber11; 01-24-2006 at 09:05 PM..
cyber11 is offline   Reply With Quote
Old 01-25-2006, 07:11 AM   PM User | #3
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
here's how i'd do it

Code:
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] + "'");
}
shyam is offline   Reply With Quote
Old 01-25-2006, 07:09 PM   PM User | #4
konithomimo
New Coder

 
Join Date: Dec 2005
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
konithomimo is an unknown quantity at this point
I would do it like this:
Code:
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:

Code:
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.

Last edited by konithomimo; 01-25-2006 at 07:13 PM..
konithomimo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:12 AM.


Advertisement
Log in to turn off these ads.