alzami Posted December 20, 2013 Share Posted December 20, 2013 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 More sharing options...
Hadien Posted December 20, 2013 Share Posted December 20, 2013 "^" is a reserved character in regEx, try escaping it first as /x/^2/g Link to comment Share on other sites More sharing options...
alzami Posted December 20, 2013 Author Share Posted December 20, 2013 (edited) thanks.but will you please explain what is this revserved character is? var rep=n.replace(/x/^2/g,"Math.pow(3,2)"); not working Edited December 20, 2013 by alzami Link to comment Share on other sites More sharing options...
Ingolme Posted December 20, 2013 Share Posted December 20, 2013 Actually, escaping is done with blackslash, not forward slash. ^ The ^ character indicates the beginning of a string in a regular expression. Link to comment Share on other sites More sharing options...
alzami Posted December 20, 2013 Author Share Posted December 20, 2013 ok.i did this. var rep1=n.replace(/x^3/g,"math.pow(3,3)"); it shows the replaced form.not evaluating value lyk this 3*math.pow(3,3) Link to comment Share on other sites More sharing options...
justsomeguy Posted December 20, 2013 Share Posted December 20, 2013 You can use the eval function to evaluate a string of code. Link to comment Share on other sites More sharing options...
alzami Posted December 20, 2013 Author Share Posted December 20, 2013 if you see my codes in post 1 you will find that i used eval function there to evaluate.not worked Link to comment Share on other sites More sharing options...
justsomeguy Posted December 20, 2013 Share Posted December 20, 2013 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 More sharing options...
alzami Posted December 20, 2013 Author Share Posted December 20, 2013 (edited) 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 December 20, 2013 by alzami Link to comment Share on other sites More sharing options...
Hadien Posted December 20, 2013 Share Posted December 20, 2013 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 More sharing options...
alzami Posted December 20, 2013 Author Share Posted December 20, 2013 (edited) what did you just do here? n.replace(/x3/g,""+Math.pow(3,3)) i mean the rule behind it(actually i am not following any books.so little explanation will be a great help) Edited December 20, 2013 by alzami Link to comment Share on other sites More sharing options...
justsomeguy Posted December 20, 2013 Share Posted December 20, 2013 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 More sharing options...
alzami Posted December 20, 2013 Author Share Posted December 20, 2013 ok thanks!that problem had been solved. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now