Solved

Search strings and store in array

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now