表示要在1或2套parens之间获取文本? [Javascript]

时间:2018-09-17 19:24:52

标签: javascript regex

我有以下内容:

POLYGON((7.593955993652344 33.70124816894531,3.1060409545898438 24.7247314453125,8.64349365234375 22.052650451660156,14.989128112792969 26.966629028320312,7.593955993652344 33.70124816894531))

我还可以拥有以下内容:

POLYGON(7.593955993652344 33.70124816894531,3.1060409545898438 24.7247314453125,8.64349365234375 22.052650451660156,14.989128112792969 26.966629028320312,7.593955993652344 33.70124816894531)

注意到括号中的差异。我只一直希望数据位于最内部的父母集中,因此我可以分割逗号。

我遇到了类似let coordFinder = /\(([^)]+)\)/g;的问题,但这两种情况并没有解决。

2 个答案:

答案 0 :(得分:-1)

您可以执行以下任一操作:

\([^()]*\)

或者,深入到数字,点和逗号

\([.,0-9 ]*\)

更新:添加的代码段(按预期工作)=>匹配具有一个或两组参数的两个版本

var poly2 = "POLYGON((7.593955993652344 33.70124816894531,3.1060409545898438 24.7247314453125,8.64349365234375 22.052650451660156,14.989128112792969 26.966629028320312,7.593955993652344 33.70124816894531))",
poly1 = "POLYGON(7.593955993652344 33.70124816894531,3.1060409545898438 24.7247314453125,8.64349365234375 22.052650451660156,14.989128112792969 26.966629028320312,7.593955993652344 33.70124816894531)";

console.log(poly1.match(/\([.,0-9 ]*\)/));
console.log(poly2.match(/\([.,0-9 ]*\)/));

答案 1 :(得分:-1)

let coordFinder = /.*\((.*?)\)/;

var test1 = '((abc(123,456)))';

var test2 = '(abc)';

console.log('result in test1: ' + test1.match(coordFinder)[1]);

console.log('result in test2: ' + test2.match(coordFinder)[1]);