Py学习  »  Jquery

Jquery-按逗号解析错误拆分的数组值

TDG • 5 年前 • 1936 次点击  

我想用逗号分隔值。

但是,根据需要,我的一个JSON返回如下值。在值内,文本有逗号。。不知道如何删除逗号和组合在一起。

var str = "South Georgia and The South Sandwich Islands,Congo, Democratic Republic,Mauritania,Finland";
var res = str.split(/(?<=\w),(?=\w)/i);
console.log(res)

在这里它按预期工作。

但是,当我在gulp包中运行这个语法时。。得到

分析错误:正则表达式无效:/(?<=\w),(?=\w)/:无效组

请告诉我是否有语法可以使用?我不能用拆分(',')来满足我们的要求。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56583
文章 [ 1 ]  |  最新文章 5 年前
CertainPerformance
Reply   •   1 楼
CertainPerformance    5 年前

在所有环境中都不支持反向查找。在这里,您可以使用单词边界,它将匹配 \w\W \W\w . (逗号是 \W -不是文字字符)

var str = "South Georgia and The South Sandwich Islands,Congo, Democratic Republic,Mauritania,Finland";
var res = str.split(/\b,\b/i);
console.log(res)

另一个(更丑的)选项是匹配一个单词字符,后跟任何字符,后跟另一个单词字符,直到lookahead匹配 ,\w 或字符串的结尾:

var str = "South Georgia and The South Sandwich Islands,Congo, Democratic Republic,Mauritania,Finland";
var res = str.match(/\w.*?\w(?=,\w|$)/g);
console.log(res)