Solved

Search strings and store in array

Posted on 2012-03-25
4
148 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquey and footrable 2 36
window.setInterval not firing 7 31
Regular Expression 1-100 with two digits 15 25
Use a select control to call a javascript function 5 31
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now