如何删除“,”来自javascript中的字符串 [英] how to remove "," from a string in javascript

查看:73
本文介绍了如何删除“,”来自javascript中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始字符串是a,d,k
我想删除所有,并将其设为adk。
我试过:a,d,k.replace(/,/,)但它不起作用。
提前致谢。

original string is "a,d,k" I want to remove all "," and make it to "adk". I tried: "a,d,k".replace(/,/,"") but it doesn't work. Thanks in advance.

推荐答案

您没有将替换方法的结果分配回您的变量。当您调用replace时,它会返回一个新字符串而不修改旧字符串。

You aren't assigning the result of the replace method back to your variable. When you call replace, it returns a new string without modifying the old one.

例如,将其加载到您最喜欢的浏览器中:

For example, load this into your favorite browser:

<html><head></head><body>
    <script type="text/javascript">
        var str1 = "a,d,k";
        str1.replace(/\,/g,"");
        var str2 = str1.replace(/\,/g,"");
        alert (str1);
        alert (str2);
    </script>
</body></html>

在这种情况下, str1 仍将是a,d,k str2 adk

In this case, str1 will still be "a,d,k" and str2 will be "adk".

如果你想改变 str1 ,你应该这样做:

If you want to change str1, you should be doing:

var str1 = "a,d,k";
str1 = str1.replace (/,/g, "");

这篇关于如何删除“,”来自javascript中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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