简体   繁体   English

正则表达式用于首次出现

[英]Regex for getting first unique occurence

I have string that looks like : 我有一个看起来像这样的字符串:

"test-file" : "abc.xml","test2-file":"abcd.xml","test3-file":"bcde.xml","test-file" : "def.xml"'

How can I create a regex that outputs an array like : 我如何创建输出如下数组的正则表达式:

{abc.xml, def.xml} or {"test-file" : "abc.xml","test-file" : "def.xml"} 

that is only pairs with test-file before ‍‍ ':' . 这只是对之前与测试文件':'

I tried : 我试过了 :

json.match(/"test-file" : "(.*)\.xml"/); 

but I am getting output: 但我得到输出:

0: "\\"test-file\\" : \\"abc.xml\\",\\"test2-file\\":\\"abcd.xml\\",\\"test3-file\\":\\"bcde.xml\\",\\"test-file\\" : \\"def.xml\\"" 0:“ \\” test-file \\“:\\” abc.xml \\“,\\” test2-file \\“:\\” abcd.xml \\“,\\” test3-file \\“:\\” bcde.xml \\“ ,\\“ test-file \\”:\\“ def.xml \\”“
​ 1: "abc.xml\\",\\"test2-file\\":\\"abcd.xml\\",\\"test3-file\\":\\"bcde.xml\\",\\"test-file\\" : \\"def" ** 1:“ abc.xml \\”,\\“ test2-file \\”:\\“ abcd.xml \\”,\\“ test3-file \\”:\\“ bcde.xml \\”,\\“ test-file \\”: \\“高清”

Store the value in a string and use split function 将值存储在字符串中并使用拆分功能

Example : 范例

test_string='"test-file" : "abc.xml","test2-file":"abcd.xml","test3-file":"bcde.xml","test-file" : "def.xml"';
test_string.split(",");

It will split that string by , and store values in an array. 它将用分割该字符串,并将值存储在数组中。

If all the key-value pairs you are looking for are 如果您要查找的所有键值对都是

  • on the same node or 在同一节点上或
  • the same level 相同水平

you should be fine using JSON directly. 您应该可以直接使用JSON。

I doubt that regex will be helpful when you have to deal with variable key names. 我怀疑当您必须处理可变键名时,正则表达式是否会有所帮助。 There must be a criterion that allows you to distinguish the good keys from the bad keys. 必须有一个标准,使您可以区分好键和坏键。

If this criterion is the order, here is a sample built around Object.keys to access the values without knowing the actual name of the key. 如果按此条件排序,这是一个围绕Object.keys构建的示例,该示例用于在不知道键的实际名称的情况下访问值。 However, there are many other ways to do this . 但是,还有许多其他方法可以做到这一点

 // Function to get the nth key from the object Object.prototype.getByIndex = function(index) { return this[Object.keys(this)[index]]; }; var json = { "config": { "files": [{ "name1": "test.xml"}, { "name2": "test2.xml" }] } }; $.each(json.config.files, function(i, v) { if (i !== 0) // or whatever is a "good" index return; //or if it is the content of the value the identifies a good value... if (v.getByIndex(0).indexOf(".xml") !== -1) { console.log(v); return; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

If you do a lot of this JSON querying operations a JSON query language/library like JSONiq , JSPath , or jsonpath could be the right thing for you. 如果您执行许多此类JSON查询操作, JSONiqJSPathjsonpath之类的JSON查询语言/库可能是您的正确选择。


If your substring/key is always the same you can indeed make use of regex, eg 如果您的子字符串/关键字始终相同,则可以使用正则表达式,例如

 const regex = /"test-file"\\s*:\\s*".*?\\.xml"/g; const str = `"test-file" : "abc.xml","test2-file":"abcd.xml","test3-file":"bcde.xml","test-file" : "def.xml"'`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM