javascript replace()不会替换所有匹配的字符 [英] javascript replace() is not replacing all the characters match

查看:47
本文介绍了javascript replace()不会替换所有匹配的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 replace()/Admin/ListOfMovies 转换为 _Admin_ListOfMovies .

var $id = id.replace('/', '_');

看起来它只替换了第一个/.如何替换所有这些?

It looks like it only replacing the first /. How do I replace all of them?

感谢您的帮助.

推荐答案

使用 regex g 标志.

Use a regex with the g flag.

var $id = id.replace(/\//g, '_');

这篇关于javascript replace()不会替换所有匹配的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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