最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript split by spaces but not those in quotes - Stack Overflow

programmeradmin3浏览0评论

The goal is to split a string at the spaces but not split the text data that is in quotes or separate that from the adjacent text.

The input is effectively a string that contains a list of value pairs. If the value value contains a space it is enclosed in quotes. I need a function that returns an array of value-pair elements as per the example below:

Example Input:

'a:0 b:1 moo:"foo bar" c:2'

Expected result:

a:0,b:1,moo:foo bar,c:2 (An array of length 4)

I have checked through a load of other questions but none of them (I found) seem to cope with my issue. Most seem to split at the space within the quotes or they split the 'moo:' and 'foo bar' into separate parts.

Any assistance would be greatly appreciated, Craig

The goal is to split a string at the spaces but not split the text data that is in quotes or separate that from the adjacent text.

The input is effectively a string that contains a list of value pairs. If the value value contains a space it is enclosed in quotes. I need a function that returns an array of value-pair elements as per the example below:

Example Input:

'a:0 b:1 moo:"foo bar" c:2'

Expected result:

a:0,b:1,moo:foo bar,c:2 (An array of length 4)

I have checked through a load of other questions but none of them (I found) seem to cope with my issue. Most seem to split at the space within the quotes or they split the 'moo:' and 'foo bar' into separate parts.

Any assistance would be greatly appreciated, Craig

Share Improve this question asked Sep 4, 2014 at 10:55 CrogCrog 1,1709 silver badges17 bronze badges 6
  • 4 possible duplicate of Regex to pick commas outside of quotes – Avinash Raj Commented Sep 4, 2014 at 11:08
  • just replace the comma with a space in the above link. – Avinash Raj Commented Sep 4, 2014 at 11:09
  • 2 Above link doesn't do what is intended, it does a replace and not a split. – Crog Commented Sep 5, 2014 at 12:42
  • There are numerous solutions but I have accepted Moob's solution as it fits perfectly into the problem scenario and actually improves the situation by removing the necessity to have quotes around values so enhances the system. – Crog Commented Sep 5, 2014 at 12:43
  • 1 Does this answer your question? javascript split string by space, but ignore space in quotes (notice not to split by the colon too) – ggorlen Commented Oct 15, 2020 at 17:48
 |  Show 1 more comment

3 Answers 3

Reset to default 16

You can use this regex for split:

var s = 'a:0 b:1 moo:"foo bar" c:2';

var m = s.split(/ +(?=(?:(?:[^"]*"){2})*[^"]*$)/g);
//=> [a:0, b:1, moo:"foo bar", c:2]

RegEx Demo

It splits on spaces only if it is outside quotes by using a positive lookahead that makes sure there are even number of quotes after a space.

You could approach it slightly differently and use a Regular Expression to split where spaces are followed by word characters and a colon (rather than a space that's not in a quoted part):

var str = 'a:0 b:1 moo:"foo bar" c:2',
    arr = str.split(/ +(?=[\w]+\:)/g);
/* [a:0, b:1, moo:"foo bar", c:2] */

Demo jsFiddle

What's this Regex doing?
It looks for a literal match on the space character, then uses a Positive Lookahead to assert that the next part can be matched:
[\w]+ = match any word character [a-zA-Z0-9_] between one and unlimited times.
\: = match the : character once (backslash escaped).
g = global modifier - don't return on first match.

Demo Regex101 (with explanation)

Any special reason it has to be a regexp?

var str = 'a:0 b:1 moo:"foo bar" c:2';

var parts = [];
var currentPart = "";
var isInQuotes= false;

for (var i = 0; i < str.length, i++) {
  var char = str.charAt(i);
  if (char === " " && !isInQuotes) {
    parts.push(currentPart);
    currentPart = "";
  } else {
    currentPart += char;
  }
  if (char === '"') {
    isInQuotes = !isInQuotes;
  }
}

if (currentPart) parts.push(currentPart);
发布评论

评论列表(0)

  1. 暂无评论