Jump to content

string.replace


alzami

Recommended Posts

why the first one works and second one doesn't work?

<!doctype html><html><head><meta charset="utf-8"><title>Untitled Document</title><style>#gap{	width:200px;	height:50px;	border:1px solid black;}</style></head><body><div id="gap"></div><p id="thiss">3*x2+4*x2</p><input type="button" value="replace this" onClick="change();"><script>function change(){	var m=document.getElementById("gap");	var n=document.getElementById("thiss").innerHTML;		var rep=n.replace(/x2/g,"Math.pow(3,2)");	m.innerHTML=rep;	var t=eval(m.innerHTML);	m.innerHTML=t;}</script></body></html>

this doesn't work. i used a ( ^ ) sign after x to clearly indicate that it is a square

<!doctype html><html><head><meta charset="utf-8"><title>Untitled Document</title><style>#gap{	width:200px;	height:50px;	border:1px solid black;}</style></head><body><div id="gap"></div><p id="thiss">3*x^2+4*x^2</p><input type="button" value="replace this" onClick="change();"><script>function change(){	var m=document.getElementById("gap");	var n=document.getElementById("thiss").innerHTML;		var rep=n.replace(/x^2/g,"Math.pow(3,2)");	m.innerHTML=rep;	var t=eval(m.innerHTML);	m.innerHTML=t;}</script></body></html>
Link to comment
Share on other sites

"^" is a reserved character in regEx, try escaping it first as /x/^2/g

Link to comment
Share on other sites

Are you suggesting that the eval function in Javascript is broken? If you pass eval a valid string of code, it's going to execute that code. Unless your browser has a non-working eval for some reason. I suggest that you verify that what you are sending to eval is valid code, and check for Javascript errors. Why do you write a value to an element's innerHTML, and then try to eval the innerHTML, instead of just using eval on the value directly? What if the browser changes the value when it goes into innerHTML?

Link to comment
Share on other sites

i am not suggesting anything.i wanted to use a ^ sign to make the statement more understandable.and i am using the string.replace function for the first time.it worked

if i just use x2 instead of x^2.

i used a backslash to seperate ^ as suggested.it shows the replaced string in the text field .not evaluating.

of course there is a mistake.i am failing to figure it out.

this simple code works fine

<!doctype html><html><head><meta charset="utf-8"><title>Untitled Document</title><style>#gap{	width:200px;	height:50px;	border:1px solid black;}</style></head><body><div id="gap"></div><p id="thiss">3*x3</p><input type="button" value="replace this" onClick="change();"><script>function change(){	var m=document.getElementById("gap");	var n=document.getElementById("thiss").innerHTML;	var rep1=n.replace(/x3/g,"3");		m.innerHTML=rep1	var t=eval(m.innerHTML);	m.innerHTML=t;}</script></body></html>

if i pass Math.pow(3,3) in stead of 3 eval function don't evaluate it.It pulish 3*Math.pow(3,3)in the screen.

 

but this script works fine....but what could be the mistakes in previous code?

<script>function change(){	var m=document.getElementById("gap");	var n=document.getElementById("thiss").innerHTML;	var rep1=eval(n.replace(/x3/g,"Math.pow(3,3)"));	m.innerHTML=rep1;}</script>
Edited by alzami
Link to comment
Share on other sites

you don't need eval, eval is pretty bad anyway. instead of writing

n.replace(/x3/g,"Math.pow(3,3)")

simply write it as:

n.replace(/x3/g,""+Math.pow(3,3))

the power function will run (best of all only once) and calculate 3^3 and then cast it into a string as "27". then it will run the replace of "27" and not "Math.pow(3,3)".

Link to comment
Share on other sites

Why are you doing this:

 

m.innerHTML=rep1

var t=eval(m.innerHTML);

m.innerHTML=t;

 

instead of this:

 

var t=eval(rep1);

m.innerHTML=t;

 

or this:

 

m.innerHTML = eval(rep1);

 

What if the browser transforms the text when you write it to innerHTML? That's what I was talking about earlier, why are you trying to use Javascript to eval innerHTML when eval is supposed to be used on Javascript code? If you run this it works perfectly fine:

 

eval("3*Math.pow(3,3)")

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...