Match between simple delimiters, but not delimiters themselves

前端 未结 2 653
悲&欢浪女
悲&欢浪女 2021-01-28 12:15

I was looking at JSON data that was just in a text file. I don\'t want to do anything aside from just use regex to get the values in between quotes. I\'m just using this as a

2条回答
  •  面向向阳花
    2021-01-28 12:50

    get the values in between quotes

    One thing to keep in mind is that valid JSON accepts escaped quotes inside the quoted values. Therefore, the RegEx should take this into account when capturing the groups which is done with the “unrolling-the-loop” pattern.

    var pattern = /"[^"\\]*(?:\\.[^"\\]*)*"/g;
    var data = {
      "value": "This is \"stuff\".",
      "empty": "",
      "null": null,
      "number": 50
    };
    var dataString = JSON.stringify(data);
    console.log(dataString);
    var matched = dataString.match(pattern);
    matched.map(item => console.log(JSON.parse(item)));

提交回复
热议问题