我有一些像下面这样的字符串
ab.ab.c,ab.d,bc.e,mt
bc.e,nmt
ef.1,mt
我想要的是用regex某种方式解析这些字符串并得到结果
例如
ab.ab.c,ab.d,bc.e,mt
val1 = ab (take the value prior to first dot)
val2 = ab.c,ab.d,bc.e (take the value after first dot but before the last comma)
val3 = mt (take value after the last comma)
相似地
bc.e,nmt
val1 = bc (take the value prior to first dot)
val2 = e (take the value after first dot but before the last comma)
val3 = nmt (take value after the last comma)
ef.1,mt
val1 = ef
val2 = 1
val3 = mt
现在我的方法更长而且效率低下。这就是我所做的
let val3 = someString.split(',').slice(-1)[0]
let remaining_string = someString.replace("," + val3, "")
let val1 = remaining_string.slice(0, remaining_string.indexOf('.'))
let val2 = null
if(remaining_string.split(",").length > 1) {
val2 = remaining_string.replace("."+val1, "")
}
else {
val2 = remaining_string.split(".")[1]
}
没有任何一个衬垫或清洁的解决方案来获得val1,val2并val3迅速?
森栏
HUWWW
叮当猫咪
相关分类