R J.
R J.

Reputation: 1542

remove unwanted space and commas in JavaScript

I want to remove all unnecessary spaces and commas from the start/end of the string.

eg; , 11,22,33 , should become 11,22,33

I tried this,

var str=",22,33,44,55,";
var splitted = str.split(",");
alert(splitted[0]);

But shows empty spaces

Upvotes: 1

Views: 854

Answers (1)

KooiInc
KooiInc

Reputation: 122936

try:

var str=",22,33,44,55,";
str.replace(/^[\s,]+|[\s,]+$/g, '').split(",");

Upvotes: 4

Related Questions