You are currently browsing the tag archive for the 'stripping off script tags' tag.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExp_SandBox {

//Pattern to escape
final static String SCRIPT_PATTERN = “<\\s*script\\s*([^>]*)\\s*>|<\\s*/\\s*script([^>]*)\\s*>”;
//Compile pattern
static Pattern scriptPattern = Pattern.compile(SCRIPT_PATTERN, Pattern.CASE_INSENSITIVE);

/**
* @param args
*/
public static void main(String[] args) {

//Source string
String css = “Hello world <p>Not replaced</p> <script id=’youfool’ name=\”youfool\”>Test this</script> <strong>this is strong</strong>”;
System.out.println( css + “\n———————————————” );

//replace all matches
css = matchAndReplace(css, scriptPattern, “*”);
System.out.println( css + “\n———————————————” );
}

public static String matchAndReplace(CharSequence message, final Pattern pattern, final String replace)
{
Matcher matcher = pattern.matcher(message);
StringBuffer buffer = new StringBuffer();
while (matcher.find())
{
matcher.appendReplacement(buffer, replace);
}
matcher.appendTail(buffer);
return buffer.toString();
}
}

Events

November 2009
S M T W T F S
« Jul    
1234567
891011121314
15161718192021
22232425262728
2930  

del.icio.us

Moments in memory

mylogo

More Photos