ios - Unable to extract individual script tags with NSRegularExpression regex -
i'm doing regex character except string on html file. i'm writing method remove script tags.
the problem looks first <script> tag , last </script> , returns 1 result. i've tried bunch of stuff can't make pick intermediary ones.
the regex'es worked out are: "<script[^>]*>((?!script).)*</script>" returns me nothing, believe closer need , "<script[^>]*>.*</script>" returns me single match.
here code. str variable string contains 3 matches. should able run code.
nsstring *str = @"<script type=\"text/javascript\">function setdisqusurl(){var _loc = window.location.href;disqus_url = _loc.split(\"?\")[0];}var disqus_url;setdisqusurl(); var disqus_shortname = 'rubydocbeta'; var disqus_shortname = 'ruby-doc'; (function() { var dsq = document.createelement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js'; (document.getelementsbytagname('head')[0] || document.getelementsbytagname('body')[0]).appendchild(dsq); })();</script><script type=\"text/javascript\">function setdisqusurl(){var _loc = window.location.href;disqus_url = _loc.split(\"?\")[0];}var disqus_url;setdisqusurl(); var disqus_shortname = 'rubydocbeta'; var disqus_shortname = 'ruby-doc'; (function() { var dsq = document.createelement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js'; (document.getelementsbytagname('head')[0] || document.getelementsbytagname('body')[0]).appendchild(dsq); })(); </script><script type=\"text/javascript\">function setdisqusurl(){var _loc = window.location.href;disqus_url = _loc.split(\"?\")[0];}var disqus_url;setdisqusurl(); var disqus_shortname = 'rubydocbeta'; var disqus_shortname = 'ruby-doc'; (function() { var dsq = document.createelement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js'; (document.getelementsbytagname('head')[0] || document.getelementsbytagname('body')[0]).appendchild(dsq); })(); </script>"; nsregularexpression *regex = [[nsregularexpression alloc] initwithpattern:@"<script[^>]*>((?!script).)*</script>" options:nsregularexpressioncaseinsensitive|nsregularexpressiondotmatcheslineseparators error:nil]; nsarray *results = [regex matchesinstring:str options:nsmatchingwithtransparentbounds range:nsmakerange(0, str.length)]; (int = 1; <= results.count; i++) { nstextcheckingresult *result = [results objectatindex:(results.count - i)]; nslog(@"======================"); nslog(@"%@", [str substringwithrange:result.range]); } nslog(@"results count: %i", (int)results.count);
<script[^>]*>.*?</script> ^^ make search non greedy.