Solved

Search strings and store in array

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

740 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