我有一个可以包含1到n值的var:
var v = `"type":"List("one","two","n")","allowNull":false,"defaultValue":"null"`
我需要用正则表达式解析它以修改所有列表元素,如下所示:
"type":"List('one element','two element','n element')","allowNull":false,"defaultValue":"null"
我做了这个正则表达式:(List()((“)([a-zA-Z0-9] *)(”)(,?)){1,}())
但我不知道如何在我的第4个捕获组内循环。
任何人都知道怎么做?
更新了regex101:https://regex101.com/r/0TnKur/4
答案 0 :(得分:2)
<强>更新强>
试试这个:
/(List\()(.*)(\))/g
(")(\w+)(")
changed string
最终替换为old string
v.replace( list, matched)
功能
var v =`"type":"List("one","two","n")","allowNull":false,"defaultValue":"null"`
var list = v.match(/(List\()(.*)(\))/g);
var matched= list[0].replace(/(")(\w+)(")/g, "\'$2 element\'");
var res =v.replace( list, matched)
console.log(res)
&#13;