Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i was trying to change js original syntax from its own js syntax, i found some syntax can be change but not all, may be an expert can do it not like newbie like me, now i m trying a different style, to input my shortcode in textarea 1 then reget those whole code as js original syntax from another textarea 2 and copy that code for pasting in onother htm's sripts tags..
in short write less get more..

here i am having problem with replace() method, i got it from w3schools.com there was only basic operation explained not complecated like this, so i posted my question here..

i want this.. as a string..

"document.write();
document.writeln();"

from this string..

d.w();
d.wl();

there are many strings but i cant write here whole.
my second problem is somehow i cant copy that code from that textarea why ?, is that my browser problem ? or it will need proper code.

What I have tried:

<script>

str = "d.w(); d.w(); d.wl();";

re = str.replace("d.", "document.");
re = str.replace("w(", "write(");
re = str.replace("wl(", "writeln(");
re = str.replace("f (", "function (");

document.write(re);

/*
this code doesnt work why ?
and what is the shortcut method for this ?
*/

</script>
Posted
Updated 2-Aug-16 19:00pm

1 solution

your code will be replacing only 1 instance of the given words.

for replacement of all instances do something like below.


C#
var str= "d.w(); d.w(); d.wl();";

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



Hope it helps. :)
 
Share this answer
 
Comments
prasad sawant 3-Aug-16 2:56am    
its work only for single str variable

means..
document.write(str); // its work

but more than double line..

document.write(str);
document.write(str); // its doesnt work,

may be because same variable

and i tried this..

replace(/d.|w(|wl(|/g, "document." write(" writeln(") but doesnt work
VICK 3-Aug-16 4:26am    
Can you please explain a bit further?
prasad sawant 3-Aug-16 6:51am    
i want to search those 3 things by this..

replace(/word1|word2|word3/g, here i want a function to replace those words to onother words);
VICK 3-Aug-16 9:04am    
And the above solution I posted is showing you how to replace two words with their replacement.. So what would be the issue in adding up the 3rd line with 3rd word.. Can you specify your exact scenario?
prasad sawant 3-Aug-16 10:26am    
i want something like this

str= "abc".replace(/a|b|c/g, "d" "e" "f");

// a as d, b as e, c as f.

ur previous code works but if i angain try to execute it, it shows blank page

means

<script>
blah blah blah..
document.write(str);
//this code will work
</script>

<script>
blah blah blah..
document.write(str);
document.write(str);
//this code is not working
may be i have a browser problem
</script>

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