1

How do I remove empty values from an comma separated string in JavaScript/jQuery?

Is there a straightforward way, or do I need to loop through it and remove them manually?

Is there a way to merge all the splits (str and str1) in JavaScript/jQuery?

CODE:

var str = '+ a + "|" + b';
var str1 = '+ a + "-" + b';

str = str.split("+").join(",").split('"|"').join(",");
str1 = str1.split("+").join(",").split('"-"').join(",");
console.log(str); //, a , , , b
console.log(str1); //, a , , , b

EXPECTED OUTPUT :

a,b

Help would be appreciated :)

2

3 Answers 3

1

As I see it, you want to remove +, "|", "-" and whitespace from the beginning and end of the string, and want to replace those within the string with a single comma. Here's three regexes to do that:

str = str.replace(/^(?:[\s+]|"[|-]")+/, '')
         .replace(/(?:[\s+]|"[|-]")+$/, '')
         .replace(/(?:[\s+]|"[|-]")+/g, ',');

The (?:[\s+]|"[|-]") matches whitespace or pluses, or "|" or "-". The + at the end repeats it one or more times. In the first expression we anchor the match to the beginning of the string and replace it with nothing (i.e. remove it). In the second expression we anchor the match to the end of the string and remove it. And in the third, there is no anchor, because all matches that are left have to be somewhere inside the string - and we replace those with ,. Note the g modifier for the last expression - without it only the first match would be replaced.

Sign up to request clarification or add additional context in comments.

5 Comments

Awesome. Worked like a charm. Thanks bro. Could you please elaborate your answer how it works? I cannot blindly copy and paste the same code. I am totally new to regular expressions. And please suggest me the link/book to learn regular expressions.
Also, if the answer is what you're looking for, you can accept it by clicking the checkmark next to it.
@user2803614 the link ChicagoRedSox posted is the learning resource for regular expressions (which I also linked to in the answer). If you have never used regular expressions before, my post would grow quite long to explain everything, so please take the time to have a read at that tutorial. The concepts I used are rather basic and are covered within the first few chapters.
@ChicagoRedSox I am not able to accept answer right away. I tried but it is showing warning called "You can accept an answer in 2 mins" Will do it sure.
@m.buettner Thanks bro. I will definitely go through the link which you and ChicagoRedSox mentioned. :)
1

The other answer is useful, and may be exactly what you are looking for.

If, for some reason, you still want to use split, luckily that method takes a regex as separator, too:

str = str.split(/\s*\+\s*(?:"\|"\s*\+\s*)?/).slice(1).join(",");

str1 = str1.split(/\s*\+\s*(?:"-"\s*\+\s*)?/).slice(1).join(",");

Because you have a plus sign in front of the "a", you can slice the array to return only the elements after it.

Also, since you mentioned you were new to regular expressions, here is the explanation:

  • any amount of space
  • a plus sign
  • any amount of space
  • optional (because of the ? after the group, which is the parentheses): a non-capturing (that is what the ?: means) group containing:
    • "|"
    • any amount of space
    • another plus sign
    • any amount of space

1 Comment

Thanks bro. But other answer, we can use in both cases. I am not sure how string (str/str1) will look like.
1

Works perfectly fine: str.split(/[ ,]+/).filter(function(v){return v!==''}).join(',')

Comments

Your Answer

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.