简体   繁体   English

Javascript RegEx全局字符串搜索下划线字符(_)

[英]Javascript RegEx global string search for underscore character(_)

I am horrible with RegEx and I have been using this online tester for some time now and still can not find what I need. 我对RegEx感到恐惧,并且我已经使用此在线测试仪已有一段时间了,但仍然找不到我需要的东西。

So I have the string "2011_G-20_Cannes_summit". 所以我有字符串“ 2011_G-20_Cannes_summit”。 I want to replace all the underscores (_) with spaces. 我想用空格替换所有下划线(_)。

So I want something like this: 所以我想要这样的事情:

var str = "2011_G-20_Cannes_summit";
str.replace(/_/g," "); or str.replace(/\_/g);

Though neither is working... 虽然都不起作用...

What am I missing? 我想念什么?

That works fine. 很好 The replace method doesn't modify the existing string, it creates a new one. replace方法不修改现有字符串,而是创建一个新字符串。 This will do what you want: 这将做您想要的:

var str = "2011_G-20_Cannes_summit";
str = str.replace(/_/g," ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM