正则表达式/ _/g是什么意思?

Tom*_*Tom 86 javascript regex

JavaScript的:

.replace(/_/g," ");
Run Code Online (Sandbox Code Playgroud)

我在我的代码中有它,但不记得它为什么或它做了什么!你们其中一个正规表达大师能帮忙吗?我知道这可能看起来很基本,但正则表达不是我的一杯茶,谷歌搜索/ g将无济于事......

SLa*_*aks 144

正则表达式匹配_角色.

g方法全球,并导致replace调用替换所有的比赛,不只是第一个.


McK*_*yla 25

像其他人一样,它用空格替换所有下划线.所以"Hello_there."会成为"Hello there."

但随着答案,我想向你提出一些建议.使用评论.

在您的代码中说出如下内容:

// Replaces all underscores so that blah blah blah blah blah..
var hello = "Hello_there."
    .replace(/_/g, ' ');
Run Code Online (Sandbox Code Playgroud)