Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove character from string using javascript

Tags:

javascript

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 anyone suggests me?

like image 741
Aarif Qureshi Avatar asked Nov 09 '12 06:11

Aarif Qureshi


1 Answers

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', '');
like image 101
Konstantin Dinev Avatar answered Nov 15 '22 19:11

Konstantin Dinev