如何在javascript中将选择不同单词的字符串替换为另一个单词? [英] How to replace strings selected different words into another words in javascript ?

查看:64
本文介绍了如何在javascript中将选择不同单词的字符串替换为另一个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从自己的js语法改变js原始语法,我发现一些语法可以改变但不是全部,可能是专家可以做的不像我这样的新手,现在我尝试不同的风格,输入我在textarea 1中的短代码然后将这些整个代码重新作为来自另一个textarea 2的js原始语法并复制该代码以粘贴在另一个htm的sripts标签上..

总之写得更少得到更多..



这里我有替换()方法的问题,我从w3schools.com得到它只有基本的操作解释没有这样的complecated,所以我在这里发布我的问题..



i想要这个..作为字符串..



document.write();

document.writeln();



来自此字符串..



dw();

d.wl();



有很多字符串,但我不能写完整篇。

我的第二个问题是某种程度上我不能从那个textarea复制那个代码为什么?是的在我的浏览器问题?或者它需要正确的代码。



我尝试过:



< script>



str =dw(); dw(); d.wl();;



re = str.replace(d。,document。);

re = str.replace(w(,write();

re = str.replace(wl(,writeln();

re = str.replace(f(,function();



document.write(重新);



/ *

这段代码不是为什么工作?

这是什么快捷方式?

* /



< / script> ;

解决方案

您的代码将只替换给定单词的1个实例。



替换所有实例都做类似下面的事情。





  var  str =   dw(); dw(); d.wl(); ; 

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







希望它有所帮助。 :)


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>

解决方案

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

for replacement of all instances do something like below.


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

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




Hope it helps. :)


这篇关于如何在javascript中将选择不同单词的字符串替换为另一个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆