Str替换 [英] Str Replace

查看:96
本文介绍了Str替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中有一个名为str_replace的函数

http:/ /php.net/str_replace)。基本上你可以用两个字符串释放

和一个主题。串。然后它通过主题字符串

搜索搜索的出现位置。字符串并替换为

replace串。 JavaScript中是否存在类似的东西,或者可以为某人提供解决方案。我是一个经验丰富的PHP用户和XHTML

作家,我已经学习了Javascript到一个合理的水平,所以你不需要
需要解释它太简单但足够简单对于程序员来说,

了解。


非常感谢,

Michael Mulqueen。

In PHP there is a function called str_replace
(http://php.net/str_replace). Basically you can freed in two strings
and a "subject" string. Then it goes through the subject string
searching for occurences of the "search" string and replaces them with
the "replace" string. Is there something simular in JavaScript, or can
someone give me a solution. I am an experienced PHP user and XHTML
writer, and I have learnt Javascript to a reasonable level so you don''t
need to explain it too simply but simple enough for a programmer to
understand.

Many Thanks,
Michael Mulqueen.

推荐答案

Michael写道:
Michael wrote:
在PHP中有一个名为str_replace的函数
http://php.net/str_replace) 。基本上你可以用两个字符串释放
和一个主题。串。然后它通过主题字符串
搜索搜索的出现。字符串并用替换替换它们。串。 JavaScript中是否有类似的东西,或者有人能给我一个解决方案。我是一位经验丰富的PHP用户和XHTML
编写者,我已经学习了Javascript到一个合理的水平,所以你不需要解释它太简单但很简单,对程序员来说
了解。
In PHP there is a function called str_replace
(http://php.net/str_replace). Basically you can freed in two strings
and a "subject" string. Then it goes through the subject string
searching for occurences of the "search" string and replaces them with
the "replace" string. Is there something simular in JavaScript, or can
someone give me a solution. I am an experienced PHP user and XHTML
writer, and I have learnt Javascript to a reasonable level so you don''t
need to explain it too simply but simple enough for a programmer to
understand.




尝试替换 String [1]对象的方法。


[1]< url:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Strin g> ;



Try the "replace" method of the String[1] object.

[1]<url:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Strin g>




Michael写道:

Michael wrote:
在PHP中有一个名为str_replace的函数
http://php.net/str_replace) 。基本上你可以用两个字符串释放
和一个主题。串。然后它通过主题字符串
搜索搜索的出现。字符串并用替换替换它们。串。 JavaScript中是否有类似的东西,或者有人能给我一个解决方案。我是一位经验丰富的PHP用户和XHTML
编写者,我已经学习了Javascript到一个合理的水平,所以你不需要解释它太简单但很简单,对程序员来说
理解。
In PHP there is a function called str_replace
(http://php.net/str_replace). Basically you can freed in two strings
and a "subject" string. Then it goes through the subject string
searching for occurences of the "search" string and replaces them with
the "replace" string. Is there something simular in JavaScript, or can
someone give me a solution. I am an experienced PHP user and XHTML
writer, and I have learnt Javascript to a reasonable level so you don''t
need to explain it too simply but simple enough for a programmer to
understand.




在JavaScript中没有完全相同的srt_replace,但是如果它可以使用RegExp工具与
完全相同需要。


如果你只需要一个快速的'''''''''''的解决方案那么:

....

var s1 =" abababababababab";

var s2 = s1.replace(/ a / g,''c'');

alert(s2); // cbcbcb ...

....


所以:subject.replace(/ searchString / g," replaceString");


/ searchString / g是带有/ g标志的正则表达式,用于替换所有

出现,而不仅仅是第一个。



There is not exact equivalent of srt_replace in JavaScript, but one can
make an exact equivalent of it using RegExp tools if it''s needed.

If you just need a quick''n''durty solution then:
....
var s1 = "abababababababab";
var s2 = s1.replace(/a/g,''c'');
alert(s2); // cbcbcb...
....

So: subject.replace(/searchString/g, "replaceString");

/searchString/g is a regular expression with /g flag to replace all
occurences, not just the first one.


谢谢,这是一个很好的建议,但它太脏了,我必须去现金洗手了:(。无论如何有帮助,但我也想知道,如果

我启动了一个blah.html的弹出窗口,从弹出窗口我怎么可以用这个来编辑主文档中的textarea?

Thankyou, that was great advice, but it is so dirty that I have to go
and wash my hands now :(. Anyway that helps but I also wondered, say if
I launched a pop up window of blah.html, from the pop up window how
could I edit a textarea in the main document with this?


这篇关于Str替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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