JS RegEx匹配引号内的逗号

时间:2018-01-16 18:20:08

标签: javascript json node.js regex

我正在尝试使用基于文本的日志文件,如下所示:

  

键入:'点击',类别:' REFRESH_DOOR',desc:'点击刷新自   门0124',网站:' mysite',路径名:' / load_areas / all / doors',参数:   '?sort_by = code& layout = view_by_4',time:' 1515838628166',mobile:   ' false',bp:' xlarge',size:' [1920,935]',area:' all',sid:   ' 15158225889',设施:' MMMM',IP地址:' XXX.XXX.XXX.XXX',   创建:' 2018年1月13日04:17'

并遍历它以从中构建JSON对象。当我在上面的条目中log.split(",")从字符串的各个部分中创建单独的对象时,它会获取日期并将其拆分,从而破坏数据。

在单引号中找到逗号的RegEx是什么,在尝试将此字符串拆分为JS数组之前,我可以替换它?

我基本上想要改变:' 2018年1月13日04:17'到1月13日; 2018 04:17',并担心以后的转换。

我试过像:

这样的表达式
'([^']*)'

它引用了引号之间的所有内容,但我还没有能够在引号之间使用逗号。

2 个答案:

答案 0 :(得分:0)

您可以先用一些不同的名称替换有问题的逗号,然后按此分割。最后,我们遍历部分并实际构建我们的对象:



var subject = "type: 'click', category: 'REFRESH_DOOR', desc: 'clicked refresh from door 0124', site: 'mysite', pathname: '/load_areas/all/doors', params: '?sort_by=code&layout=view_by_4', time: '1515838628166', mobile: 'false', bp: 'xlarge', size: '[1920, 935]', area: 'all', sid: '15158225889', facility: 'MMMM', IP Address: 'XXX.XXX.XXX.XXX', created: 'January 13, 2018 04:17'";

var regex = /'[^']+'|(,)/g;
var kv = /(\w+):\s*'([^']+)'/;

replaced = subject.replace(regex, function(m, group1) {
    if (typeof group1 == 'undefined') return m;
    else return 'SUPERMAN';
});

// create an object that holds our values
var jsonObject = {};

// create a function that splits the individual items
function splitter(item) {
	var match = kv.exec(item);
  jsonObject[match[1]] = match[2];
}

parts = replaced.split("SUPERMAN");
parts.forEach(splitter);
console.log(jsonObject);



 产量:

{
  "type": "click",
  "category": "REFRESH_DOOR",
  "desc": "clicked refresh from door 0124",
  "site": "mysite",
  "pathname": "/load_areas/all/doors",
  "params": "?sort_by=code&layout=view_by_4",
  "time": "1515838628166",
  "mobile": "false",
  "bp": "xlarge",
  "size": "[1920, 935]",
  "area": "all",
  "sid": "15158225889",
  "facility": "MMMM",
  "Address": "XXX.XXX.XXX.XXX",
  "created": "January 13, 2018 04:17"
}

<小时/> 改编自The Best Regex Trick

答案 1 :(得分:0)

我将正则表达式稍微更改为/'.*?'/g,因此它会在引号内选择任何内容,然后我们只需替换它来查找所有逗号并用分号替换它们。

&#13;
&#13;
var str = "type: 'click', category: 'REFRESH_DOOR', desc: 'clicked refresh from door 0124', site: 'mysite', pathname: '/load_areas/all/doors', params: '?sort_by=code&layout=view_by_4', time: '1515838628166', mobile: 'false', bp: 'xlarge', size: '[1920, 935]', area: 'all', sid: '15158225889', facility: 'MMMM', IP Address: 'XXX.XXX.XXX.XXX', created: 'January 13, 2018 04:17'";

//Select all quoted strings...
var replacedCommas = str.replace(/'.*?'/g, function(match){
  //Now replace any commas found in each match with semicolons
  return match.replace(/,/g, ';');
});

console.log(replacedCommas);
&#13;
&#13;
&#13;