如何从可能被可选逗号字符拆分的字符串中提取数据?

How does one extract data from a string which possibly could get split by an optional comma character?

我得到了像 "Street number 1, Barcelona" 这样的字符串,或者如果没有街道数据,只有 "Barcelona",我正在尝试通过单个正则表达式表达模式和捕获城市数据。

我已经想出了一个模式,它 select 不包含逗号的字符串上的所有内容 ...

^([^,]+)$

我还有一个正则表达式,用于捕获第一个逗号后的所有内容...

^.+?, (.*)$

是否可以将两者合并成一个正则表达式and/or如何管理它?

应该这样做:

^(?:.+,\s)?(.*)$

这里有一个link来验证:

https://regex101.com/r/Hak486/1/

刚刚测试

如果 OP 的用例只是测试,遵循正则表达式可能会有所帮助 /^[^,]*,*[^,]+$/。它如下...

  • /^ ... $/ 在开始和结束之间...
  • [^,]* 可选择匹配任何 不是逗号 ...
  • ,* 可能后跟一个 可选逗号 ...
  • [^,]+ 后跟(或仅代表其自身)至少一个 不是逗号 .
  • 的字符

const regXOptionallySplittingComma = (/^[^,]*,*[^,]+$/);

console.log(
  'test ... "" ?',
  regXOptionallySplittingComma.test("")
);
console.log(
  'test ... "," ?',
  regXOptionallySplittingComma.test(",")
);
console.log(
  'test ... " ," ?',
  regXOptionallySplittingComma.test(" ,")
);
console.log('\n');

console.log(
  'test ... " , " ?',
  regXOptionallySplittingComma.test(" , ")
);
console.log(
  'test ... ",  " ?',
  regXOptionallySplittingComma.test(",  ")
);
console.log(
  'test ... "   " ?',
  regXOptionallySplittingComma.test("   ")
);
console.log('\n');

console.log(
  'test ... "  Barcelona  " ?',
  regXOptionallySplittingComma.test("  Barcelona  ")
);
console.log(
  'test ... "Lloret de Mar" ?',
  regXOptionallySplittingComma.test("Lloret de Mar")
);
console.log('\n');

console.log(
  'test ... ",  Barcelona  " ?',
  regXOptionallySplittingComma.test(",  Barcelona  ")
);
console.log(
  'test ... ",  Lloret de Mar  " ?',
  regXOptionallySplittingComma.test(",  Lloret de Mar  ")
);
console.log('\n');

console.log(
  'test ... "Street number 1, Barcelona" ?',
  regXOptionallySplittingComma.test("Street number 1,  Barcelona")
);
console.log(
  'test ... "Street number 1, Lloret de Mar" ?',
  regXOptionallySplittingComma.test("Street number 1,  Lloret de Mar")
);
console.log('\n');

console.log(
  'test ... "Street, number 1, Barcelona" ?',
  regXOptionallySplittingComma.test("Street, number 1,  Barcelona")
);
console.log(
  'test ... "Street number 1, Lloret de, Mar" ?',
  regXOptionallySplittingComma.test("Street number 1,  Lloret de, Mar")
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

提取数据

如果 OP 的用例是提取数据,例如获取可以用可选逗号分隔的字符串的右侧,则可能的解决方案不一定需要基于正则表达式...

function extractTrailingNonCommaChars(str) {
  const list = String(str).split(',');

  // - without testing for a maximum two partials split
  // - take always the last or most right handed partial ...
  // return list[list.length - 1].trim();

  // - with testing for a maximum two partials split ...
  const count = list.length;
  return (count <= 2) && list[count - 1].trim() || '';
}

console.log(
  '"Street number 1,  Barcelona  " =>',
  `"${ extractTrailingNonCommaChars("Street number 1,  Barcelona  ") }"`
);
console.log(
  '",  Lloret de Mar  " =>',
  `"${ extractTrailingNonCommaChars(",  Lloret de Mar  ") }"`
);
console.log(
  '" Barcelona" =>',
  `"${ extractTrailingNonCommaChars(" Barcelona") }"`
);
console.log(
  '"Lloret, de Mar" =>',
  `"${ extractTrailingNonCommaChars("Lloret, de Mar") }"`
);

console.log(
  '",,," =>',
  `"${ extractTrailingNonCommaChars(",,,") }"`
);
console.log(
  '",  " =>',
  `"${ extractTrailingNonCommaChars(",  ") }"`
);
console.log(
  '" , " =>',
  `"${ extractTrailingNonCommaChars(" , ") }"`
);
console.log(
  '"   " =>',
  `"${ extractTrailingNonCommaChars("   ") }"`
);
console.log(
  '"," =>',
  `"${ extractTrailingNonCommaChars(",") }"`
);
console.log(
  '"" =>',
  `"${ extractTrailingNonCommaChars("") }"`
);
console.log(
  '"Street, number 1,  Barcelona  " =>',
  `"${ extractTrailingNonCommaChars("Street, number 1,  Barcelona  ") }"`
);
console.log(
  '",  Lloret de, Mar  " =>',
  `"${ extractTrailingNonCommaChars(",  Lloret de, Mar  ") }"`
);
.as-console-wrapper { min-height: 100%!important; top: 0; }