【发布时间】:2018-08-19 03:38:38
【问题描述】:
我的字符串看起来像:
"test-file" : "abc.xml","test2-file":"abcd.xml","test3-file":"bcde.xml","test-file" : "def.xml"'
如何创建一个输出数组的正则表达式:
{abc.xml, def.xml} or {"test-file" : "abc.xml","test-file" : "def.xml"}
那只是在 ':' 之前与测试文件配对。
我试过了:
json.match(/"test-file" : "(.*)\.xml"/);
但我得到了输出:
0: "\"test-file\" : \"abc.xml\",\"test2-file\":\"abcd.xml\",\"test3-file\":\"bcde. xml\",\"测试文件\" : \"def.xml\""
1: "abc.xml\",\"test2-file\":\"abcd.xml\",\"test3-file\":\"bcde.xml\",\"test-file\":\ “定义”
【问题讨论】:
-
{abc.xml, def.xml}这是一个无效的对象。or {"test-file" : "abc.xml","test-file" : "def.xml"}这将导致只有一个键值对。 -
@CertainPerformance 感谢您的更正,但是这种问题是否有可能的正则表达式?
-
@wp78de 这个 json 子树可能是带有可变键的更大 json 的一部分,所以不要调用 '.values;反复(因为 json 结构未知),我想将 json 转换为字符串并使用正则表达式查找特定的键值对。有可能吗?
-
要得到你想要的结果,请
.*reluctant:"test-file" : "(.*?\.xml)
标签: javascript regex