how to remove “,” from a string in javascript

后端 未结 6 480
我在风中等你
我在风中等你 2020-12-24 00:03

original string is \"a,d,k\" I want to remove all , and make it to \"adk\".

I tried code below but it doesn\'t work.

6条回答
  •  自闭症患者
    2020-12-24 00:43

    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:

    
        
    
    

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

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

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

提交回复
热议问题