6

I have a variable in javascript with text separated with commas, for example:

var array = "hello,goodbye,test1,test2,1,90m tall"; [NO SPACE AFTER COMMAS]

If I use .split(','); the result is as follows:

array[0] = hello
array[1] = goodbye
array[2] = test1 
array[3] = test2
array[4] = 1
array[5] = 90m tall

But I want this:

array[0] = hello
array[1] = goodbye
array[2] = test1 
array[3] = test2
array[4] = 1,90m tall

How can I do it? I can imagine I have to add some special restriction but I don't know how...I also checked regex but...no success.

7
  • Use a . Instead of , for numbers? Commented Apr 16, 2016 at 16:55
  • @evolutionxbox It's just an example, in real life it's a big variable with long text (500KB, > 9000lines) so I can't just replace it with . Commented Apr 16, 2016 at 16:57
  • Use the , answers, but only if you can guarantee that's how the commas are separated. Commented Apr 16, 2016 at 17:00
  • @evolutionxbox I edited the main post, it's my fault, no spaces after comma! Commented Apr 16, 2016 at 17:01
  • 1
    In the future make sure you use real code. You just invalidated 2 answers that were correct and that's not cool. Commented Apr 16, 2016 at 17:06

5 Answers 5

8

It depends on what the logic is by which you want to split or not split. If for instance you don't want to split by a comma that has at least two digits following it, then this will do it:

array.split(/,(?!\d\d)/))

This performs a negative look ahead for two digits, and will only match commas which do not have these digits following them.

Snippet:

var array = "hello,goodbye,test1,test2,1,90m tall";

document.body.innerHTML = array.split(/,(?!\d\d)/)
                               .join('<br>');

Follow up question/answer

You asked in comments how to split this:

[hello]blabla,[how,are,you]xx,[i'm fine]

So that a comma would only split if it were followed by an opening bracket.

For this you use positive look ahead (?=):

var array = "[hello]blabla,[how,are,you]xx,[i'm fine]";

document.body.innerHTML = array.split(/,(?=\[)/)
                               .join('<br>');

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

5 Comments

I'm looking for a regex similar, but instead of only 2 digits, I want only match comma which do not have digits or any char. For example: var array = "hello,goodbye,test1,test2,some text with,comma in the middle"; This is another possible scenario. Then the result should be: array[4] = some text with,comma in the middle
If you do that then the comma after test2 will not be split either (if it is followed by ,1,90. Is this what you want? Please be very exact. comma which do not have ... is not clear. Do you mean before or after? You were clear in your question, but what you say here would not solve the problem you put in the question.
More simple: each sentence have [ ] symbol at the beggining. For example: var array = "[hello]blabla,[how,are,you]xx,[i'm fine]"; Expected result: array[0] = [hello]blabla, array[1] = [how,are,you]xx, array[2] = [i'm fine] So I want to split the text ONLY if after comma, there is a '[' symbol.
Well, that is a different question. You would split by /,(?=\[)/, where you do a positive lookahead, meaning the bracket must follow the comma, otherwise it is not a splitting comma.
Exactly! Awesome, do you use any tool for regex? It's working now.
3

Use .split(', ') (note the included space)

Edit: Based on your change to the question, trincot's solution does what you're asking for.

Here's a JSFiddle to prove it: https://jsfiddle.net/ea2mdgpj/

2 Comments

It's not working because the string it's not separated with spaces, I'm gonna edit the main post. It's my fault, I'm sorry.
Ah. Then you have a problem.
2

If the string really has spaces after each comma-separated field, just change the split call:

.split(', ');

Comments

1

You can do it like,

var array = "hello,goodbye,test1,test2,1,90m tall";
array = array.split(",");
array.push(array.splice(array.length - 1).join(","));

And now console.log(array) will be,

["hello", "goodbye", "test1", "test2", "1,90m tall"]

Concept behind the logic,

  • split the string by using the delimiter ,
  • detach the last two cells from the array using splice.
  • attach the detached part into the original array by joining with ,

2 Comments

What if there are elements after the 90m tall element in other rows?
@NidhinDavid OP might have a pattern for his array. If the array that he given is his constant pattern then this code would work.
0

var array = "hello, goodbye, test1, test2, 1,90m tall";

Add the spaces and use .split(', '), or use dots ('.') for numbers instead of commas (',')

1 Comment

I wish, but is not that simple.

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.