0

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

1
  • Try if (!valueList[i]) instead of if (valueList[i] == "") Commented May 31, 2014 at 6:22

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Instead of using replace I want it logical.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.