Reputation: 15
Working on XSS vulnerability in which I have a requirement to remove all the leading special characters only. No need to remove the special characters at the end or in between. Alphanumeric is allowed but no special characters at the beginning.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
public static void main(String args[]) {
String str= "**$*****Important****\n"
+ " document is not uploaded \n"
+ "%3Cscript%3E";
str = str.replaceAll("[^a-zA-Z0-9\\s]", " ");
System.out.println(str);
}
}
The code above gives output as:
Important
But the expected output is:
Important**** document is not uploaded 3Cscript%3E
How can I fix this?
Upvotes: 0
Views: 333
Reputation: 4296
You probably need
str = str.replaceAll("(?m)^[^a-zA-Z0-9\\s]+", " ");
Upvotes: 0
Reputation: 83527
There are three changes you need to make:
^
.+
.""
instead of " "
: str = str.replaceAll("^[^a-zA-Z0-9\\s]+", "");
Upvotes: 2