I am trying to inject the replace method for javascript in webview for android.
This code works:
{
mWebView.loadUrl("javascript:(function(){" +
"document.body.innerHTML = document.body.innerHTML.replace('hello', 'hi');" +
"})()");
}
Instead of putting the string in the method, however, I want to use variables. I tried using regex but it does not seem to work.
{
String old = "hello";
String new = "hi";
mWebView.loadUrl("javascript:(function(){" +
"var ol = new RegExp(old,'g');" +
"document.body.innerHTML = document.body.innerHTML.replace(ol, new);" +
"})()");
}
Is there something off with my code?
new RegExp(" + old + ",'g')and.replace(ol, " + new + ");