Remove character from string using javascript

前端 未结 7 2168
失恋的感觉
失恋的感觉 2020-12-11 16:03

i have comma separated string like

var test = 1,3,4,5,6,

i want to remove particular character from this string using java script

can a

7条回答
  •  余生分开走
    2020-12-11 17:04

    JavaScript strings provide you with replace method which takes as a parameter a string of which the first instance is replaced or a RegEx, which if being global, replaces all instances.

    Example:

    var str = 'aba';
    str.replace('a', ''); // results in 'ba'
    str.replace(/a/g, ''); // results in 'b'
    

    If you alert str - you will get back the same original string cause strings are immutable. You will need to assign it back to the string :

    str = str.replace('a', '');
    

提交回复
热议问题