javascript - 用多个逗号分割字符串
问题描述
它是如何在文本下方拆分的?它包含逗号分隔值,但一些内部值也有逗号。但是我们知道每个组都以GO:XX
模式开始。
GO:0048193, BP, 高尔基体囊泡转运, GO:0030198, BP, 细胞外基质组织, GO:0006903, BP, 囊泡靶向, GO:0043062, BP, 细胞外结构组织, GO:0048199, BP, 囊泡靶向, to,来自或在高尔基体内部, GO:0031012, CC, 细胞外基质, GO:0062023, CC, 含胶原蛋白的细胞外基质, GO:0005581, CC, 胶原蛋白三聚体, GO:0044420, CC, 细胞外基质成分, GO:0030020, MF , 赋予抗张强度的细胞外基质结构成分, GO:0005201, MF, 细胞外基质结构成分
我使用了这个正则表达式模式,但不适用于多逗号值:(如在 GO:0048199 中)
let myRegexp = /(GO:[0-9]+), (BP|MF|CC), ([^,]+)/g;
let raw = "GO:0048193, BP, Golgi vesicle transport, GO:0030198, BP, extracellular matrix organization, GO:0006903, BP, vesicle targeting, GO:0043062, BP, extracellular structure organization, GO:0048199, BP, vesicle targeting, to, from or within Golgi, GO:0031012, CC, extracellular matrix, GO:0062023, CC, collagen-containing extracellular matrix, GO:0005581, CC, collagen trimer, GO:0044420, CC, extracellular matrix component, GO:0030020, MF, extracellular matrix structural constituent conferring tensile strength, GO:0005201, MF, extracellular matrix structural constituent"
let match = myRegexp.exec(raw);
while (match != null) {
console.log(match[0].trim());
match = myRegexp.exec(raw);
}
也许我可以使用模式拆分数据:GO:[0-9]+
但是我无法捕获 GO ID。这将是两个步骤两个捕获所有数据,所以它是丑陋的代码。有没有更好的解决方案?
解决方案
您可以使用前瞻:
GO:\d+.*?(?=,\s+GO:|$)
在 regex101.com 上查看演示。
这JS
可能是:
let myRegexp = /GO:\d+.*?(?=,\s+GO:|$)/g;
let raw = "GO:0048193, BP, Golgi vesicle transport, GO:0030198, BP, extracellular matrix organization, GO:0006903, BP, vesicle targeting, GO:0043062, BP, extracellular structure organization, GO:0048199, BP, vesicle targeting, to, from or within Golgi, GO:0031012, CC, extracellular matrix, GO:0062023, CC, collagen-containing extracellular matrix, GO:0005581, CC, collagen trimer, GO:0044420, CC, extracellular matrix component, GO:0030020, MF, extracellular matrix structural constituent conferring tensile strength, GO:0005201, MF, extracellular matrix structural constituent"
let match = myRegexp.exec(raw);
while (match != null) {
console.log(match[0].trim());
match = myRegexp.exec(raw);
}
推荐阅读
- python-3.x - 3D 绘图仪仅适用于某些形状
- java - 从 PDF 中无损提取图像
- jq - 如何使用 jq 过滤 JSON 对象以仅选择特定的键/值?
- android - 我在 ionic 中编译构建 android 时出错
- r - 用于将数据集字典/元数据中的信息匹配到主集中的行操作
- javascript - Why isn't my POST XMLHttpRequest.Send working?
- c# - Azure 搜索合并索引操作返回异常
- android - 在 Android 中进行 AR 开发是否有 ArCore 的替代方案?
- tensorflow - 在训练模型时,您能否将某些数据点的权重小于其他数据点?
- javascript - 避免根据显示的图像调整幻灯片 div 的大小?