0

I just want to remove contents inside script tag when i look into the console for result.

now it is showing informations along with correct results.

<script type="text/javascript">
	
	
	var texta=document.documentElement.innerHTML;
	
	
	var result = texta.match(/(.*)[:](.*)/g);
	var rex = /(<([^>]+)>)/ig;
	var htmltag=[];
	//console.log(result)
	for (index = 0; index < result.length; ++index) {
	     //console.log(index)
	     htmltag[index]=strip_html_tags(result[index]);
	     console.log(htmltag[index]. split(/[\<>&=:]/));
	
	} 


	function strip_html_tags(str)
	{
	   if ((str===null) || (str===''))
	       return false;
	  else
	   str = str.toString();
	  return str.replace(/<[^>]*>/g, '');
	}
	
</script>
<html><li>Product Code : Product 15</li>
<li>Product Points : 100</li>
<span>Product : In Stock</span>
</html>

In console i am getting

  1. ["Product Code ", " Product 15"]
  2. ["Product Points ", " 100"]
  3. ["Product", "In Stock"]
  4. [" var result ", " texta.match(/(.*)", "/g);"]
  5. [" console.log(htmltag[index]. split(/[\", "", "", "]/));"]

i do not want 4th and 5th as it is come from inside javascript.

10
  • Do you want to remove any script tag or just this one ? Commented Jun 14, 2017 at 6:00
  • any javascript script tags Commented Jun 14, 2017 at 6:01
  • can you be very specific what content do you want to remove?(keeping above output in mind) Commented Jun 14, 2017 at 6:04
  • @HameedSyed please check now. Commented Jun 14, 2017 at 6:10
  • You just want to have the content of your li tags into an array ? if so, try yo use document.getElementsByTagName('li') Commented Jun 14, 2017 at 6:13

2 Answers 2

1

I see a couple of issues with your code.

See my jsfiddle demo, where I've adjusted your code a bit.

JS

<script>
  var texta=document.getElementsByClassName('details');

  for(var i=0; i <= texta.length; i++) {

  var result = texta[i].innerHTML.match(/(.*)[:](.*)/g);
  var rex = /(<([^>]+)>)/ig;
  var htmltag=[];

  for (index = 0; index < result.length; ++index) {
     htmltag[index]=strip_html_tags(result[index]);
     console.log(htmltag[index]. split(/[\<>&=:]/));
  } 
}
function strip_html_tags(str)
{
    if ((str===null) || (str===''))
        return false;
    else
        str = str.toString();
    return str.replace(/<[^>]*>/g, '');
}

</script>

HTML

<body>
<ul class="details">
<li>Product Code : Product 15</li>
<li>Product Points : 100</li>
<li>Product = In Stock</li>
</ul>
<div class="details">
   <div>Product Code : Product 15</div>
   <div>Product Points : 100</div>
   <div>Product = In Stock</div>
</div>
</body>

Output

(2) ["Product Code ", " Product 15"]
(2) ["Product Points ", " 100"]

Your now able to add a css class called "details" and all the content of each "details" dom content and apply a regex match.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi dude, cool share for him but its what is looking for : "anything that matches regular expression not just li tag" I edited your post
thanks but not just li tag.It may come from span tag too.
Corrected, the JavaScript in the post works perfectly, waiting for OP to accept my edition.
0

It is working.Thanks everyone.

<script type="text/javascript">
	
	var texta=document.documentElement.innerHTML;

	function strip_html_tags(str)
	{
	   if ((str===null) || (str===''))
	       return false;
	  else
	   str = str.toString();
	  return str.replace(/<[^>]*>/g, '');
	}

	var sratscript = texta.indexOf("script")-1;
	var endscript = texta.lastIndexOf("script")+1;
	var arr = texta.split('');
	var removed = arr.splice(sratscript,endscript," "); // THistr 
	texta = arr.join('');
	//console.log(texta )
	var result = texta.match(/(.*)[:](.*)/g);
	var rex = /(<([^>]+)>)/ig;
	var htmltag=[];
	for (index = 0; index < result.length; ++index) {
	     //console.log(index)
	     htmltag[index]=strip_html_tags(result[index]);
	    
		console.log(htmltag[index]. split(/[\<>&=:]/))
	} 

</script>
<html><li>Product Code : Product 15</li>
<li>Product Points : 100</li>
<span>Product : In Stock</span>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.