【发布时间】:2017-02-27 21:21:48
【问题描述】:
我有以下字符串
133. Alarm (Peep peep)
我的目标是使用正则表达式将字符串拆分为 3 部分并将其存储为 json 对象,例如
{
"id": "133",
"Title": "Alarm",
"Subtitle": "Peep peep"
}
我可以得到号码使用
function getID(text){
let numberPattern = /\d+/g;
let id = title.match(numberPattern);
if(id){
return id[0];
}
}
大括号之间的文本使用
function getSubtitle(text){
let braces = /\((.*)\)/i;
let subtitle = title.match(braces);
if(subtitle){
return subtitle[1];
}
}
我想知道是否可以使用单个正则表达式从字符串中获取三个值(假设我会将其应用于该字符串形状的长列表)
【问题讨论】:
标签: javascript regex