为什么在Javascript中使用字符串替换时需要添加/ g? [英] Why do i need to add /g when using string replace in Javascript?

查看:71
本文介绍了为什么在Javascript中使用字符串替换时需要添加/ g?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在JavaScript中使用字符串替换时需要'/ g'?

Why is the '/g' required when using string replace in JavaScript?

例如。 var myString = myString.replace(/%0D%0A / g,< br />);

推荐答案

这不是必需的,但在JavaScript中默认 string.replace 只会替换它找到的第一个匹配值。添加 / g 意味着将替换所有匹配值。

It isn't required, but by default string.replace in JavaScript will only replace the first matching value it finds. Adding the /g will mean that all of the matching values are replaced.

这篇关于为什么在Javascript中使用字符串替换时需要添加/ g?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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