How can i turn string into array with .split but ignore quote in js2019 Community Moderator ElectionHow do I iterate over the words of a string?How do I check if an array includes an object in JavaScript?How do I read / convert an InputStream into a String in Java?How to append something to an array?How do I split a string on a delimiter in Bash?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?

Life insurance that covers only simultaneous/dual deaths

Why did it take so long to abandon sail after steamships were demonstrated?

Is it true that real estate prices mainly go up?

2D counterpart of std::array in C++17

What are the possible solutions of the given equation?

Could the Saturn V actually have launched astronauts around Venus?

How is the Swiss post e-voting system supposed to work, and how was it wrong?

Do I need life insurance if I can cover my own funeral costs?

How do I hide Chekhov's Gun?

Does this AnyDice function accurately calculate the number of ogres you make unconcious with three 4th-level castings of Sleep?

RegionDifference for Cylinder and Cuboid

What are some nice/clever ways to introduce the tonic's dominant seventh chord?

Co-worker team leader wants to inject his friend's awful software into our development. What should I say to our common boss?

How to explain that I do not want to visit a country due to personal safety concern?

What is the greatest age difference between a married couple in Tanach?

PlotLabels with equations not expressions

How could a female member of a species produce eggs unto death?

Rejected in 4th interview round citing insufficient years of experience

Identifying the interval from A♭ to D♯

What options are left, if Britain cannot decide?

Does this property of comaximal ideals always holds?

How do anti-virus programs start at Windows boot?

How could a scammer know the apps on my phone / iTunes account?

Bash: What does "masking return values" mean?



How can i turn string into array with .split but ignore quote in js



2019 Community Moderator ElectionHow do I iterate over the words of a string?How do I check if an array includes an object in JavaScript?How do I read / convert an InputStream into a String in Java?How to append something to an array?How do I split a string on a delimiter in Bash?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?










1















So I'm using .split(" ") in js to turn a string into array by whitespaces in JS. But let's say now I have the following string: Howdy "How are you" bro, how can I split the string by whitespaces but ignore things inside quotes (both single and double). So I can come out with something like this:



[
"Howdy",
""How are you"",
"bro"
]









share|improve this question

















  • 3





    This can get messy quickly. What if there's only a single quote? Can quotes be escaped? What about Foo (bar "baz) blarg"?

    – deceze
    Mar 7 at 12:38






  • 1





    I think here you have to first split the string at the " (and ') and then split the remaining strings at the space again. The challenge here is to preserve the order of the split strings then.

    – cloned
    Mar 7 at 12:38











  • @cloned I can successful keep the quoted text with str = str.split('"');, but then while I try to split it with (i=0;i<str.length;i++) str[i] = str[i].split(" ");, it'll become nested array. And the space behind Howdy and before the quote, for example, turns into a blank value within the nested array. (Like this "[["Howdy",""],["How","are","you"],["","bro."]]")

    – Andrew-at-TW
    Mar 7 at 12:47
















1















So I'm using .split(" ") in js to turn a string into array by whitespaces in JS. But let's say now I have the following string: Howdy "How are you" bro, how can I split the string by whitespaces but ignore things inside quotes (both single and double). So I can come out with something like this:



[
"Howdy",
""How are you"",
"bro"
]









share|improve this question

















  • 3





    This can get messy quickly. What if there's only a single quote? Can quotes be escaped? What about Foo (bar "baz) blarg"?

    – deceze
    Mar 7 at 12:38






  • 1





    I think here you have to first split the string at the " (and ') and then split the remaining strings at the space again. The challenge here is to preserve the order of the split strings then.

    – cloned
    Mar 7 at 12:38











  • @cloned I can successful keep the quoted text with str = str.split('"');, but then while I try to split it with (i=0;i<str.length;i++) str[i] = str[i].split(" ");, it'll become nested array. And the space behind Howdy and before the quote, for example, turns into a blank value within the nested array. (Like this "[["Howdy",""],["How","are","you"],["","bro."]]")

    – Andrew-at-TW
    Mar 7 at 12:47














1












1








1








So I'm using .split(" ") in js to turn a string into array by whitespaces in JS. But let's say now I have the following string: Howdy "How are you" bro, how can I split the string by whitespaces but ignore things inside quotes (both single and double). So I can come out with something like this:



[
"Howdy",
""How are you"",
"bro"
]









share|improve this question














So I'm using .split(" ") in js to turn a string into array by whitespaces in JS. But let's say now I have the following string: Howdy "How are you" bro, how can I split the string by whitespaces but ignore things inside quotes (both single and double). So I can come out with something like this:



[
"Howdy",
""How are you"",
"bro"
]






javascript arrays string split






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 12:35









Andrew-at-TWAndrew-at-TW

12112




12112







  • 3





    This can get messy quickly. What if there's only a single quote? Can quotes be escaped? What about Foo (bar "baz) blarg"?

    – deceze
    Mar 7 at 12:38






  • 1





    I think here you have to first split the string at the " (and ') and then split the remaining strings at the space again. The challenge here is to preserve the order of the split strings then.

    – cloned
    Mar 7 at 12:38











  • @cloned I can successful keep the quoted text with str = str.split('"');, but then while I try to split it with (i=0;i<str.length;i++) str[i] = str[i].split(" ");, it'll become nested array. And the space behind Howdy and before the quote, for example, turns into a blank value within the nested array. (Like this "[["Howdy",""],["How","are","you"],["","bro."]]")

    – Andrew-at-TW
    Mar 7 at 12:47













  • 3





    This can get messy quickly. What if there's only a single quote? Can quotes be escaped? What about Foo (bar "baz) blarg"?

    – deceze
    Mar 7 at 12:38






  • 1





    I think here you have to first split the string at the " (and ') and then split the remaining strings at the space again. The challenge here is to preserve the order of the split strings then.

    – cloned
    Mar 7 at 12:38











  • @cloned I can successful keep the quoted text with str = str.split('"');, but then while I try to split it with (i=0;i<str.length;i++) str[i] = str[i].split(" ");, it'll become nested array. And the space behind Howdy and before the quote, for example, turns into a blank value within the nested array. (Like this "[["Howdy",""],["How","are","you"],["","bro."]]")

    – Andrew-at-TW
    Mar 7 at 12:47








3




3





This can get messy quickly. What if there's only a single quote? Can quotes be escaped? What about Foo (bar "baz) blarg"?

– deceze
Mar 7 at 12:38





This can get messy quickly. What if there's only a single quote? Can quotes be escaped? What about Foo (bar "baz) blarg"?

– deceze
Mar 7 at 12:38




1




1





I think here you have to first split the string at the " (and ') and then split the remaining strings at the space again. The challenge here is to preserve the order of the split strings then.

– cloned
Mar 7 at 12:38





I think here you have to first split the string at the " (and ') and then split the remaining strings at the space again. The challenge here is to preserve the order of the split strings then.

– cloned
Mar 7 at 12:38













@cloned I can successful keep the quoted text with str = str.split('"');, but then while I try to split it with (i=0;i<str.length;i++) str[i] = str[i].split(" ");, it'll become nested array. And the space behind Howdy and before the quote, for example, turns into a blank value within the nested array. (Like this "[["Howdy",""],["How","are","you"],["","bro."]]")

– Andrew-at-TW
Mar 7 at 12:47






@cloned I can successful keep the quoted text with str = str.split('"');, but then while I try to split it with (i=0;i<str.length;i++) str[i] = str[i].split(" ");, it'll become nested array. And the space behind Howdy and before the quote, for example, turns into a blank value within the nested array. (Like this "[["Howdy",""],["How","are","you"],["","bro."]]")

– Andrew-at-TW
Mar 7 at 12:47













1 Answer
1






active

oldest

votes


















4














One possibility is to think about this in terms of matching rather than splitting. You can match things between quotes or words in that order with something like:






let s = 'Howdy "How are you" bro'
let a = s.match(/".+?"|S+/g)
console.log(a)





If the examples become more complicated (for example nested quotes), of course, this might require some adjustments.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55043953%2fhow-can-i-turn-string-into-array-with-split-but-ignore-quote-in-js%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    One possibility is to think about this in terms of matching rather than splitting. You can match things between quotes or words in that order with something like:






    let s = 'Howdy "How are you" bro'
    let a = s.match(/".+?"|S+/g)
    console.log(a)





    If the examples become more complicated (for example nested quotes), of course, this might require some adjustments.






    share|improve this answer



























      4














      One possibility is to think about this in terms of matching rather than splitting. You can match things between quotes or words in that order with something like:






      let s = 'Howdy "How are you" bro'
      let a = s.match(/".+?"|S+/g)
      console.log(a)





      If the examples become more complicated (for example nested quotes), of course, this might require some adjustments.






      share|improve this answer

























        4












        4








        4







        One possibility is to think about this in terms of matching rather than splitting. You can match things between quotes or words in that order with something like:






        let s = 'Howdy "How are you" bro'
        let a = s.match(/".+?"|S+/g)
        console.log(a)





        If the examples become more complicated (for example nested quotes), of course, this might require some adjustments.






        share|improve this answer













        One possibility is to think about this in terms of matching rather than splitting. You can match things between quotes or words in that order with something like:






        let s = 'Howdy "How are you" bro'
        let a = s.match(/".+?"|S+/g)
        console.log(a)





        If the examples become more complicated (for example nested quotes), of course, this might require some adjustments.






        let s = 'Howdy "How are you" bro'
        let a = s.match(/".+?"|S+/g)
        console.log(a)





        let s = 'Howdy "How are you" bro'
        let a = s.match(/".+?"|S+/g)
        console.log(a)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 12:48









        Mark MeyerMark Meyer

        39.2k33162




        39.2k33162





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55043953%2fhow-can-i-turn-string-into-array-with-split-but-ignore-quote-in-js%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived