replace()方法用于将字符串中的一些字符替换成其他字符

 

Definition and Usage
定义与用法

The replace() method is used to replace some characters with some other characters in a string.
replace()方法用于将字符串中的一些字符替换成其他字符

Syntax
语法

stringObject.replace(findstring,newstring)
Parameter
参数
Description
注释
findstring
目标字符串
Required. Specifies a string value to find. To perform a global search add a 'g' flag to this parameter and to perform a case-insensitive search add an 'i' flag
必选项。指定所要替换的字符串。要执行多次匹配需要添加一个”g“标记。要指定模糊匹配需要添加一个”i“标记
newstring
新字符串
Required. Specifies the string to replace the found value from findstring
必选项。指定所要替换的字符串的新值

Tips and Notes
注意

Note: The replace() method is case sensitive.
注意:replace()方法是精确匹配的


Example 1 - Standard Replace
实例 1 - 标准替换

In the following example we will replace the word Microsoft with w3pop:
在下面的例子中,我们将把Microsoft替换成w3pop:

<script type="text/javascript">
var str="这里是Micosoft!"
document.write(str.replace(/Microsoft/, "w3pop"))
</script>

The output of the code above will be:
输出结果为:

这里是w3pop!

Note: In the following example the word Microsoft will not be replaced (because the replace() method is case sensitive):
注意:在下面的例子中,Microsoft将不会被替换(replace()方法是精确匹配的)

<script type="text/javascript">
var str="这里是Microsoft!"
document.write(str.replace(/microsoft/, "w3pop"))
</script>

The output of the code above will be:
输出结果为:

这里是Microsoft!

Example 2 - Case-insensitive Search
实例 2 - 模糊匹配

In the following example we will perform a case-insensitive search, and the word Microsoft will be replaced:
在下面的例子中,我们将演示一个模糊匹配,Microsoft将会被替换:

<script type="text/javascript">
var str="这里是Microsoft!"
document.write(str.replace(/microsoft/i, "w3pop"))
</script>

The output of the code above will be:
返回结果为:

这里是w3pop!

Example 3 - Global Search
实例3 - 多次匹配

In the following example we will perform a global match, and the word Microsoft will be replaced each time it is found:
在下面的例子中,我们将演示一个多次匹配:

<script type="text/javascript">
var str="欢迎来到Microsoft! "
str=str + "Microsoft将为你提供全面的技术支持"
document.write(str.replace(/Microsoft/g, "w3pop"))
</script>

The output of the code above will be:
输出结果为:

欢迎来到w3pop! w3pop将为你提供全面的技术支持

Example 4 - Global and Case-insensitive Search
实例 4 - 多次模糊匹配

In the following example we will perform a global and case-insensitive match, and the word Microsoft will be replaced each time it is found, independent of upper and lower case characters:
在下面的例子中,我们将演示多次模糊匹配:

<script type="text/javascript">
var str="欢迎来到Microsoft! "
str=str + "Microsoft将为你提供全面的技术支持"
document.write(str.replace(/microsoft/gi, "w3pop"))
</script>

The output of the code above will be:
输出结果为:

欢迎来到w3pop! w3pop将为你提供全
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟V-SpHeNIC

支持科研技术

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值