I am new to both JavaScript and Regular Expression. I have a simple task to replace a series of <%whatever%> into (whatever) within the web page. Somehow regExp works in certain conditions only.
I am not sure if I've got the Expression wrong or the way I try to access document.body.HTML wrong. Please note the "works" and "not working" remarks within the code.
<html>
<head>
<script>
</script>
</head>
<body>
Hello, my items are <%abc%>, <%efg%>, <%hik%><p/>
<p/>
</body>
<script>
var Transform = function() {
var regExp = /<%\^?\w+?\.?\w+?%>/gi;
document.body.innerHTML = document.body.innerHTML.replace(/Hello/g,"Hi"); //works
document.body.innerHTML = document.body.innerHTML.replace(/my/g,"your"); //works
document.body.innerHTML = document.body.innerHTML.replace(/\<%/g,"("); //not working
document.body.innerHTML = document.body.innerHTML.replace(/%\>/g,")"); //not working
return;
}
teststr="<%abc%>";
teststr=teststr.replace(/\<%/g,"(");
teststr=teststr.replace(/%\>/g,")"); //works show as (abc)
console.log(teststr)
Transform();
</script>
</html>
%isn't a regex special character. (<and>too)