简体   繁体   English

如何在javascript中替换字符串中的字符

[英]how replace a character in a string in javascript

i have a string : 我有一个字符串:

var s="here_we_go";

how can i replace all occurrence of '_' by '$' so the string will become: 我如何用“ $”替换所有出现的“ _”,所以字符串将变为:

s="here$we$go";

Use .replace .... 使用.replace ....

s.replace(/_/g, '$');

The g here ensures every (not just one) occurrences of _ will be replaced by $ . 这里的g确保_每一次(不仅是一次)都会被$代替。

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

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