A
ast
hi
I am trying to replace the character ^ in a string with character %
using "replace" string method. I know that ^ is a special character
so it has to be backslashed. But it doesnt work.
example
<html>
<body>
<script type="text/javascript">
var s = "x^12+2";
var s2 = "";
var reg = new RegExp("\^", "g");
s2 = s.replace(reg, "%" );
document.write(s2);
</script>
</body>
</html>
It provide %x^12+2 instead of x%12+2
I tried many variations
var reg = new RegExp("^", "g");
var reg = new RegExp("/^", "g");
var reg = new RegExp('^', "g");
but nothing works.
Same behavior with IE8 and firefox 3.6.8
Any idea ?
I am trying to replace the character ^ in a string with character %
using "replace" string method. I know that ^ is a special character
so it has to be backslashed. But it doesnt work.
example
<html>
<body>
<script type="text/javascript">
var s = "x^12+2";
var s2 = "";
var reg = new RegExp("\^", "g");
s2 = s.replace(reg, "%" );
document.write(s2);
</script>
</body>
</html>
It provide %x^12+2 instead of x%12+2
I tried many variations
var reg = new RegExp("^", "g");
var reg = new RegExp("/^", "g");
var reg = new RegExp('^', "g");
but nothing works.
Same behavior with IE8 and firefox 3.6.8
Any idea ?