Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 19-05-2009, 09:17 PM   #1 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default String match in javascript


Hello guys, I am stuck here.
The textarea element has some text value like this: Ak, Am, Co, Cfp, Ce, Cfm, Ao, ... etc. As you can see each significant value is separated by a comma.
I want to parse through this long string and extract each value separately into an array. I mean the array should be like this:

Code:
myarray[0]="Ak"
myarray[1]="Am"
myarray[2]="Co"
myarray[3]="Cfp"
myarray[4]="Ce"
myarray[5]="Cfm"
myarray[6]="Ao"
and so on. How can this be done in javascript? I know this can be done using regex pattern but I have no idea how to do it.
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 20-05-2009, 01:07 AM   #2 (permalink)
Broken In
 
Join Date: Aug 2008
Location: Mumbai, India
Posts: 169
Default Re: String match in javascript

I am not sure if you are looking for a regex only solution. Here is a non-regex solution that should work for you:

Code:
<HTML>
<head>
<script language="JavaScript">
	function testFunc()
	{
		var txt = document.abc.txtA1.value;
		var tmpTxt = '';
		var i=0;
		var retArr = new Array();
		while(txt.indexOf(",") >= 0)
		{
			tmpTxt = txt.substring(0, txt.indexOf(","));
			retArr[i++] = tmpTxt;
			txt = txt.substring(txt.indexOf(tmpTxt) + tmpTxt.length + 1, txt.length);
		}
		retArr[i++] = txt;
		alert('Your final array is: ' + retArr);
	}
	</script>
</head>
<body>
	<form name="abc">
		<input type='text' name="txtA1" id="txtA1" size=60 />
		<input type='button' name="butt1" value="To Array" onClick="testFunc();"/>
	</form>
</body>
</HTML>
Bandu is offline  
Old 20-05-2009, 02:05 PM   #3 (permalink)
Super Hero - Super Powers
 
n2casey's Avatar
 
Join Date: Sep 2006
Location: Dynamic
Posts: 766
Cool Re: String match in javascript

It can be done through regex also. Solution is just one line code.

Code:
var val = document.getElementById('textbox1').value;
        var arr = val.split(/,/g);
 
        for(i=0; i < arr.length; i++)
           alert(arr[i]);
In the above code, arr will be an array of resulting values.
__________________
Minds are like Parachutes :arrow: They work best when open
n2casey is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Sujeet
- by gforz
- by soumya

Advertisement




All times are GMT +5.5. The time now is 03:13 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2