Solved

Search strings and store in array

Posted on 2012-03-25
4
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

726 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