The elements in my list should be A1,A2,A3,A4
If user input A1,A2,A3,A4,,,,,, or A1,A2,,,A3,A4,,A5,, or A,B, ,, ,, V,,,, , , , , ,ddfd ,,,,,,,,
It should consider as
A1,A2,A3,A4
The logic written by me was
if(valueText !== null) { alert("Value Text..." + valueText);
valueList = valueText.split(",");
for (var i = 0; i < valueList.length; i++)
{
if (valueList[i] == "")
{
valueList.splice(i, 1);
alert("ValueList inside for if.."+valueList);
}
}
alert("ValueList.." + valueList);
}
But its not working properly
-
Try if (!valueList[i]) instead of if (valueList[i] == "")etherous– etherous2014年05月31日 06:22:21 +00:00Commented May 31, 2014 at 6:22
2 Answers 2
You can do something like this with match & join functions:-
var str = "A1,A2,,,A3,A4,,A5,,";
strnew = str.match(/[^ ,]+/g).join(',');
//Output--> A1,A2,A3,A4,A5
Hope this will help you...
answered May 31, 2014 at 6:34
Akshay Paghdar
3,6292 gold badges24 silver badges45 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can do this with regex, for example:
var txt = 'A1,A2,,,A3,A4,,A5,,'
var res = txt.replace(/(,)1円*/g, ',').replace(/,$/, '');
//^ A1,A2,A3,A4,A5
answered May 31, 2014 at 6:23
elclanrs
94.2k21 gold badges137 silver badges171 bronze badges
1 Comment
user3687566
Instead of using replace I want it logical.
lang-js