// include the latest version of the regex crate in your Cargo.toml
extern crate regex;
use regex::Regex;
fn main() {
let regex = Regex::new(r#"\s*<script src="\/((\w+)[\w.\/]+)"><\/script>"#).unwrap();
let string = "<!DOCTYPE html>
<html>
<head>
<meta charset=\"utf-8\">
<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'\">
<meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\">
<title>atomic-project</title>
</head>
<body>
<div id=\"app\"></div>
<!-- built files will be auto injected -->
<script src=\"/app.js\"></script><script src=\"/app.js\"></script><script src=\"/app.js\"></script><script src=\"/app.js\"></script></body>
</html>
";
let substitution = "\\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";
// result will be a String with the substituted value
let result = regex.replace_all(string, substitution);
println!("{}", 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 Rust, please visit: https://docs.rs/regex/latest/regex/