Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to change this string..

str = "d.w('download');"

to this string..

document.write('download');

somehow its returns..
d.write('dowritenload');

here i dont want to change letters between '' or "" marks and returns d as document.

i tried to find it on w3schools. there are every code's sample but i dont know how to arrange those code with each other in my script ?

What I have tried:



<script>

str = "d.w('download');"

re = str.replace(/d/g, "document");
re = str.replace(/w/g, "write");

document.getElementById("demo").innerHTML=re;

/*
this code returns..
d.write('dowritenload');

but i want this..

document.write('download');

means i don't want to change letters between '' or "" marks and want d as document.
*/

/*
1) i know the problem is with multiple variable "re" which only returns last variable's answer so how to create that as one ?

2) i found on w3schools this which can help me..

re = str.replace(/(d|w)/g, how to set more than one words in this parameter ?);

3) there are 3 d's are in my string but i dont want change those 2 d's in quoted marks.

*/

</script>
Posted
Updated 5-Aug-16 3:50am

C#
str = "d.w('download');"

re = str.replace(/d/g, "document");
re = str.replace(/w/g, "write");


You may wish to restrict your replace to the first incidence of 'd' and 'w'. Check your regular expression syntax and figure it out from there.




Eg., back to your w3Schools reference: RegExp g Modifier[^]
 
Share this answer
 
v3
try

C#
str = "d.w('download');"
str = str.replace('d.w', "document.write");

// or
str = str.replace('d.', "document.");
str = str.replace('w(', "write(");
 
Share this answer
 
Comments
prasad sawant 5-Aug-16 12:06pm    
but what if d. and "download." both d. are same and they will replace as "downloadocument." i want the answer for this how to not change words between quotes.
prasad sawant 5-Aug-16 12:10pm    
those both str variable not execute at same time only last variables value returns the answer, i want single variable for executing.
Karthik_Mahalingam 5-Aug-16 12:16pm    
str = str.replace('d.', "document.").replace('w(', "write(");
prasad sawant 5-Aug-16 12:16pm    
"d.w(" is perfect i also used it many times but i scared what will happen if it comes again in strings so how can i remove it or stope it. like

d.w("d.w(");

it will return

document.write("document.write(");
Karthik_Mahalingam 5-Aug-16 12:29pm    
try this

var str = 'd.w("d.w(")'
var temp = str.substring(0,5).replace('d.w','document.write')
var content = str.substring(5,str.lastIndexOf(')' )) + ')'
str = temp + content

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900