import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\s*<script src=\"\\/((\\w+)[\\w.\\/]+)\"><\\/script>";
final String string = "<!DOCTYPE html>\n"
+ "<html>\n\n"
+ "<head>\n"
+ " <meta charset=\"utf-8\">\n"
+ " <meta http-equiv=\"Content-Security-Policy\" content=\"default-src * data: gap: https://ssl.gstatic.com; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'\">\n"
+ " <meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\">\n"
+ " <title>atomic-project</title>\n"
+ "</head>\n\n"
+ "<body>\n"
+ " <div id=\"app\"></div>\n"
+ " <!-- built files will be auto injected -->\n\n"
+ "<script src=\"/app.js\"></script><script src=\"/app.js\"></script><script src=\"/app.js\"></script><script src=\"/app.js\"></script></body>\n\n"
+ "</html>\n";
final String subst = "\\n<script>\\n const $2Script = document.createElement('script');\\n $2Script.type = 'text/javascript';\\n $2Script.async = true;\\n $2Script.src = '$1';\\n document.body.appendChild($2Script);\\n</script>\\n";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html