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?
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
add a comment |
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
3
This can get messy quickly. What if there's only a single quote? Can quotes be escaped? What aboutFoo (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 withstr = 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
add a comment |
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
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
javascript arrays string split
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 aboutFoo (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 withstr = 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
add a comment |
3
This can get messy quickly. What if there's only a single quote? Can quotes be escaped? What aboutFoo (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 withstr = 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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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)
answered Mar 7 at 12:48
Mark MeyerMark Meyer
39.2k33162
39.2k33162
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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