Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7619919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:48:40+00:00 2026-05-31T03:48:40+00:00

You would think this one has been asked before but I cant find it.

  • 0

You would think this one has been asked before but I cant find it.

I need to separate a js string by un quoted commas. I’m only using double quotes so that should make it a bit simpler.

I have tried two approaches but not nailed it.

I need to turn this:

'body.loaded"who, are , you" div"hello ,"#div-id span CODE, body.loaded span"span, text" code'

into this:

[
 'body.loaded"who, are , you" div"hello ,"#div-id span CODE',
 'body.loaded span"span, text" code'
]

1) -> match the good parts, which mostly works but gives me allot of empty strings in my result.

'body.loaded"who, are , you" div"hello ,"#div-id span CODE, body.loaded span"span, text" code'.match(
  /([^,]*"[^"]*")*/g
)

['body.loaded"who, are , you" div"hello' ,'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ' body.loaded span"span, text"', '', '', '', '', '', '']

I think its because of the () in the regex.

2) split the bad parts, which isnt quite there yet. The idea here is to match commas followed by an even number of “.

'body.loaded"who, are , you" div"hello ,"#div-id span CODE, body.loaded span"span, text" code'.split(
    /,(?![^"]*"[^"]*("[^"]*"[^"]*)*$)/
);

Basically, there has to be a cleaner simpler and more beautiful solution (bear in mind javascript does not support look behinds).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:48:41+00:00Added an answer on May 31, 2026 at 3:48 am

    Assuming you don’t support escapes in your double-quoted strings, this should probably work:

    /(?:"[^"]*"|[^,])+/g
    

    If you do want to support backslash-escapes inside of double-quoted strings, this should do the job:

    /(?:"(?:\\.|[^"])*"|[^,])+/g
    

    If you want to support backslash-escapes outside of double-quoted strings too (e.g. escaping the initial quote), then try this:

    /(?:"(?:\\.|[^"])*"|\\.|[^,])+/g
    

    Here’s an explanation for how the third pattern works.

    First, an expanded, annotated version:

    (?:      # start a non-capturing group
      "      # Match a double quote
      (?:    # Another non-capturing group, for the contents of the double-quote
        \\.  # Match any backslash-escaped character
      | [^"] # or any non-double-quote character
      )*     # End the group. Repeat zero or more times
      "      # Close double quote
    |        # Alternative to double-quoted string
      \\.    # Match any escaped character
    |        # Another alternative
      [^,]   # Match any non-comma character
    )+       # Close group, repeat one or more times
    

    There’s three primary components here.

    The first is to match any double-quoted string. This comes first in the group because if a double-quoted string can possibly match here, it should, as opposed to using the non-comma rule. Inside this double-quoted string we can match either any escaped character (\\.), which lets us escape double-quotes inside the string, or we match any non-double-quote character. We only match one character at a time so as to not catch escapes with the non-double-quote character rule. The contents of the string use * because double-quoted strings may be empty, and then we terminate the string.

    Instead of a double-quoted string, we may just match any escaped character (\\.). This lets us escape a double-quote character while outside of a double-quoted string. It actually lets us escape a comma too, which I’m not sure if you want. If you don’t want it, this rule should turn into \\[^,] instead.

    And lastly, if we can’t match a double-quoted string here, and we can’t match an escape, just match any non-comma character. This is not repeated so as to not catch later double-quotes or escapes with this rule.

    Then we go ahead and repeat the entire pattern with the + modifier. This lets us match more than one token at a time. We use + instead of * to avoid returning empty strings in our result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't think this has been asked before on this site, but I might
This question has been asked before, but I would like a little more detail
I know this has been asked before , but I don't think these solutions
sorry if this questions is overly basic or has been asked before but I
I think my question has been asked here before, I did read them but
I don't know if this has been asked before, but what i'd like to
i'm pretty sure that this question has allready been asked (but i didn't find
I know this question has been asked before, but I feel it wasn't asked
Apologies if this has been asked before. I searched and couldn't find it. In
This has been asked few times but I think it's still worth checking with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.