Link to home
Start Free TrialLog in
Avatar of WestCoast_BC
WestCoast_BCFlag for Canada

asked on

I have a string and I want to remove everything from and including '<style' to /style>

Can I use ReReplaceNoCase or someother javascript function to  remove everything in a string from and including '<style' to '/style>'? What would the function call look like?
Avatar of HainKurt
HainKurt
Flag of Canada image

what is input string
and
what output you look for

give samples pls
Avatar of WestCoast_BC

ASKER

Input string is something like:

<style type="text/css">.span_2_of_3 {
      width: 66.13%;
}

.span_1_of_3 {
      width: 22.26%;
}
</style>
Here is some sample text.
<style type="text/css">.span_2_of_3 {
      width: 66.13%;
}

.span_2_of_3 {
      width: 22.26%;
}
</style>

and I just want:
Here is some sample text.
Avatar of n2fc
<!DOCTYPE html>
<html>
<body>

<p>Click the button to trim the  string.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "Hello world, <style with junk /style> welcome to the universe.";
    var n1 = str.indexOf("<style");
    var n2 = str.indexOf("/style>");    
    
    
    document.getElementById("demo").innerHTML =
       str.substr(0, n1) + str.substr(n1+6, n2-n1-6) + str.substr(n2+7, str.length);
  }
</script>

</body>
</html>

Open in new window

thanks but this doesn't solve my problem. I may have a string that looks like:
var str = "part a <style>here is junk style</style>part b<style>more junk style</style>part c"

and the function would return:
part a part b part c
I have implemented something like the following that seems to work:
function stripHTMLCont(str) 
{
	var strTempReplace = encodeForHTML(str);
	var n1 = FindNoCase ("&lt;style", strTempReplace);
	var n2 = FindNoCase ("style&gt;", strTempReplace, n1+7);
	while ((n1 gt 0) and (n2 gt 0))
	{
		strTemp = Left(strTempReplace, n1) & Right(strTempReplace, n2+7);
		strTempReplace = strTemp;
		n1 = FindNoCase ("&lt;style", strTempReplace);
		n2 = FindNoCase ("style&gt;", strTempReplace, n1+6);
	}
	
	return strTempReplace ;
}

Open in new window

SOLUTION
Avatar of n2fc
n2fc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As a matter of interest, I ran a time comparison of the jQuery approach vs regex - latter was in the region of 150x faster.
150x faster

how many times you tried?
how many times you tried?
? - you mean if I try enough times they will be the same? Or are you asking on the iteration required to determine the difference.
Thank you! I will try the regular expression solution that was provided by Julian Hansen above. That is exactly what I was looking for.
Any chance you can give me your solution using cfscript and the function REReplace?
The question is in the JavaScript TA - where is the overlap with cfscript?
Sorry, I realized after asking the question that I need my problem solved using CFScript.
@Julian

As a matter of interest, I ran a time comparison of the jQuery approach vs regex - latter was in the region of 150x faster.

and here is demo for the solution provided here
ID: 42159670
https://jsfiddle.net/y79Lrhu1/

it takes 36ms for 1000 iterations!

https://jsfiddle.net/0s5oLjox/

Are you preparing for Guinness records :)

so basically, are you saying, regex will take 1 ms for 5000 iterations?!
@WestCoast_BC
I suggest you close this question and open another as there is no relationship between the two.
all 3 solutions fit to requirement but jQuery version is simplest...
all 3 solutions fit to requirement but jQuery version is simplest...
Based on what exactly?
You are trying to argue that
var fixStr = $(str).remove('style').empty().text();

Open in new window

Is simpler than
var x = e.replace(/<style[\s\S]+?<\/style>/gi,'');

Open in new window

When
a) The first solution requires an external library (hence a dependency)
b) The first solution runs significantly slower than the second

I have to disagree with you on that based on the above and by the author's comment here (https://www.experts-exchange.com/questions/29026748/I-have-a-string-and-I-want-to-remove-everything-from-and-including-'-style'-to-style.html?anchorAnswerId=42160288#a42160288)
@Julian

almost everybody uses jQuery already on their side...
and jQuery is simple to understand for most people than regular expression...
and performance is not an issue if you are not doing this millions of times in a loop...