Solved

Search strings and store in array

Posted on 2012-03-25
4
151 Views
Last Modified: 2012-03-28
Hello and sorry for my english!

I need to check if a string is present, and how many times, looking for certain html tags and store some strings into an array (if present).

example:

var='<body><p class="hello">Hello 1</p><ul><li>1</li><li>2</li></ul><p class="hello">Hello 2</p><ul><li>3</li><li>4</li></ul><p class="hello">Hello 3</p><ul><li>5</li><li>6</li></ul></body>';

Open in new window


in that case what I need is:

result[0] Hello 1
result[1] <ul><li>1</li><li>2</li></ul>
result[2] Hello 2

and so...

thank you
0
Comment
Question by:eMisc
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Ronak Patel
ID: 37764798
Hi eMisc,

If the format of input string will not going to change, then below code will work fine.


function generateArray()
{
	var txt = '<body><p class="hello">Hello 1</p><ul><li>1</li><li>2</li></ul><p class="hello">Hello 2</p><ul><li>3</li><li>4</li></ul><p class="hello">Hello 3</p><ul><li>5</li><li>6</li></ul></body>';
	
	txtNoBody = txt.substring(txt.indexOf("<body>")+6, txt.indexOf("</body>"));
	
	txtNoP = txtNoBody;
	
	for(i=0; i < txtNoP.length; i++)
	{
		txtNoP = txtNoP.replace('<p class="hello">','|');
		txtNoP = txtNoP.replace('</p>','|');
	}
	
	arr = txtNoP.split("|");
	
	result = new Array();
	index = 0;
	for(i = 0; i < arr.length; i++)
	{
		if(arr[i] != "")
		{
			result[index] = arr[i];
			index++;
		}
	}
	
	var resultArray = "";

	for(i = 0; i < result.length; i++)
	{
		resultArray += "result[" + i + "]: " + result[i] + "\n\n";
	}
	
	alert(resultArray);
}

Open in new window

0
 

Author Comment

by:eMisc
ID: 37769123
Hello Ronak_Patel,
I can't make it work, quotes are stripped out from html code:

var txt = '<div class=zipcodes staggeredList><p class=letterInfo>Palabras de 4 Letras Scrabble</p><ul><li>halo</li><li>hola</li></ul><p></p><p class=letterInfo>Palabras de 3 Letras Scrabble</p><ul><li>aho</li><li>hao</li><li>loa</li><li>ola</li></ul><p class=letterInfo>Palabras de 2 Letras Scrabble</p><ul><li>ah</li><li>al</li><li>ha</li><li>la</li><li>lo</li><li>oh</li></ul></div>'
	
	txtNoBody = txt.substring(txt.indexOf("<div class=zipcodes staggeredList>")+34, txt.indexOf("</div>"));
alert(txtNoBody);
	
	txtNoP = txtNoBody;
	
	for(i=0; i < txtNoP.length; i++)
	{
		txtNoP = txtNoP.replace('<p class=letterInfo>','|');
		txtNoP = txtNoP.replace('</ul>','|');
	}
	
	arr = txtNoP.split("|");
	
	result = new Array();
	index = 0;
	for(i = 0; i < arr.length; i++)
	{
		if(arr[i] != "")
		{
			result[index] = arr[i];
			index++;
                        alert(arr[i]); //debug
		}
	}
	
	var resultArray = "";

	for(i = 0; i < result.length; i++)
	{
		resultArray += "result[" + i + "]: " + result[i] + "\n\n";
	}
	
	document.write(resultArray);

Open in new window

0
 
LVL 5

Accepted Solution

by:
Ronak Patel earned 500 total points
ID: 37770531
here...the only change I did in your code is:

replace txtNoP = txtNoP.replace('</ul>','|'); with txtNoP = txtNoP.replace('</p>','|'); on line #11.

I've attached the output result also... take a look and let me know if you got what you wanted or not ! :)

var txt = '<div class=zipcodes staggeredList><p class=letterInfo>Palabras de 4 Letras Scrabble</p><ul><li>halo</li><li>hola</li></ul><p></p><p class=letterInfo>Palabras de 3 Letras Scrabble</p><ul><li>aho</li><li>hao</li><li>loa</li><li>ola</li></ul><p class=letterInfo>Palabras de 2 Letras Scrabble</p><ul><li>ah</li><li>al</li><li>ha</li><li>la</li><li>lo</li><li>oh</li></ul></div>'
	
	txtNoBody = txt.substring(txt.indexOf("<div class=zipcodes staggeredList>")+34, txt.indexOf("</div>"));
	//alert(txtNoBody);
	
	txtNoP = txtNoBody;
	
	for(i=0; i < txtNoP.length; i++)
	{
		txtNoP = txtNoP.replace('<p class=letterInfo>','|');
		txtNoP = txtNoP.replace('</p>','|');
	}
	
	arr = txtNoP.split("|");
	
	result = new Array();
	index = 0;
	for(i = 0; i < arr.length; i++)
	{
		if(arr[i] != "")
		{
			result[index] = arr[i];
			index++;
            alert(arr[i]); //debug
		}
	}
	
	var resultArray = "";

	for(i = 0; i < result.length; i++)
	{
		resultArray += "<br><br>result[" + i + "]: " + result[i];
	}
	
	document.write(resultArray);

Open in new window



Result:
0
 

Author Comment

by:eMisc
ID: 37778156
great! :)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question