使用javascript在浏览器中解析grep的结果

使用javascript在浏览器中解析grep的结果,javascript,linux,browser,grep,Javascript,Linux,Browser,Grep,我想在浏览器中解析grep命令的结果。 类似于grep-nriI“hello”myFolder的东西 结果是一个多行字符串: /home/user/folder/file1:1:hello world /home/user/folder/file2:1:world hello /home/user/folder/folder/file3:1:bonjour=hello /主/用户/文件夹/文件1:1:hello world /home/user/folder/file2:1:world你好 /h

我想在浏览器中解析grep命令的结果。 类似于grep-nriI“hello”myFolder的东西 结果是一个多行字符串:

/home/user/folder/file1:1:hello world /home/user/folder/file2:1:world hello /home/user/folder/folder/file3:1:bonjour=hello /主/用户/文件夹/文件1:1:hello world /home/user/folder/file2:1:world你好 /home/user/folder/folder/file3:1:bonjour=hello 首先,我将行拆分为一个数组。并使用以下正则表达式解析它:
/^(.*?\:(\d*)\:(.*?$/

我有一些问题

  • 对于具有类似于双点(:)的有趣字符的结果,解析将不起作用
  • 当我grep一个文件时,我不会得到
    pah:line number:content
    ,而只得到
    line number:content
    ,因此它使正则表达式更加复杂(javascript正则表达式中没有命名组)
  • 有人已经有了一个好的解析器或者一个解析它的项目。它必须在浏览器中工作

    我将制作一个JSFIDLE。

    代码:

    var regex = /^([^:]*):([^:]*):([^:]*)$/;
    var lines = [
        '/home/user/folder/file1:1:hello world',
        '/home/user/folder/file2:1:world hello',
        '/home/user/folder/folder/file3:1:bonjour=hello'
    ];
    var output = $('#output');
    
    for(var i in lines)
    {
        var result = lines[i].match(regex).slice(1);
    
        output.append(result.join(' - ') + "\n");
    }
    
    结果:

    /home/user/folder/file1 - 1 - hello world /home/user/folder/file2 - 1 - world hello /home/user/folder/folder/file3 - 1 - bonjour=hello /home/user/folder/file1-1-hello world /home/user/folder/file2-1-world hello /home/user/folder/folder/file3-1-bonjour=hello 对我来说很好,这可能意味着我不理解你的问题。希望这能有所帮助。JSFiddle:

    上述正则表达式匹配后的
    切片(1)
    用于删除数组中的第一个结果,即完全匹配。

    代码:

    var regex = /^([^:]*):([^:]*):([^:]*)$/;
    var lines = [
        '/home/user/folder/file1:1:hello world',
        '/home/user/folder/file2:1:world hello',
        '/home/user/folder/folder/file3:1:bonjour=hello'
    ];
    var output = $('#output');
    
    for(var i in lines)
    {
        var result = lines[i].match(regex).slice(1);
    
        output.append(result.join(' - ') + "\n");
    }
    
    结果:

    /home/user/folder/file1 - 1 - hello world /home/user/folder/file2 - 1 - world hello /home/user/folder/folder/file3 - 1 - bonjour=hello /home/user/folder/file1-1-hello world /home/user/folder/file2-1-world hello /home/user/folder/folder/file3-1-bonjour=hello 对我来说很好,这可能意味着我不理解你的问题。希望这能有所帮助。JSFiddle:

    上述正则表达式匹配后的
    片段(1)
    是为了去除数组中的第一个结果,即完全匹配。

    我的grep(在Ubuntu Linux上)有一些选项可能会有所帮助,尽管它们都不是POSIX标准

    对于不明确的输出:

       -Z, --null
              Output  a  zero  byte  (the ASCII NUL character) instead of the character
              that normally follows a file name.  For example, grep -lZ outputs a  zero
              byte  after  each  file  name  instead of the usual newline.  This option
              makes the  output  unambiguous,  even  in  the  presence  of  file  names
              containing  unusual  characters  like  newlines.  This option can be used
              with commands like find -print0, perl  -0,  sort  -z,  and  xargs  -0  to
              process arbitrary file names, even those that contain newline characters.
    
    对于缺少的文件名:

       -H, --with-filename
              Print the file name for each match.  This is the default  when  there  is
              more than one file to search.
    
    因此,使用grep-nriIHZ并将您的正则表达式更新为类似以下内容(未测试):

    我的grep(在ubuntulinux上)有一些选项可以帮助我,尽管它们都不是POSIX标准的

    对于不明确的输出:

       -Z, --null
              Output  a  zero  byte  (the ASCII NUL character) instead of the character
              that normally follows a file name.  For example, grep -lZ outputs a  zero
              byte  after  each  file  name  instead of the usual newline.  This option
              makes the  output  unambiguous,  even  in  the  presence  of  file  names
              containing  unusual  characters  like  newlines.  This option can be used
              with commands like find -print0, perl  -0,  sort  -z,  and  xargs  -0  to
              process arbitrary file names, even those that contain newline characters.
    
    对于缺少的文件名:

       -H, --with-filename
              Print the file name for each match.  This is the default  when  there  is
              more than one file to search.
    
    因此,使用grep-nriIHZ并将您的正则表达式更新为类似以下内容(未测试):


    还不错,但当我只grep一个文件时会出现问题。我可以测试结果中的第二项是否为空<代码>'1:hello world'给出了
    [“1”,“hello world”]
    请参见此处:为什么您的正则表达式更好?它也失败了,路径为冒号:
    /home/user/folder/fil:e1
    。谢谢:我认为你应该考虑用冒号无效的文件名——至少为了你自己的理智。还请注意,我使用的正则表达式比您的简单,只需匹配三个不包含“:”的组:“不错,但当我grep一个文件时会出现问题。我可以测试结果中的第二项是否为空<代码>'1:hello world'给出了
    [“1”,“hello world”]
    请参见此处:为什么您的正则表达式更好?它也失败了,路径为冒号:
    /home/user/folder/fil:e1
    。谢谢:我认为你应该考虑用冒号无效的文件名——至少为了你自己的理智。还请注意,我使用的正则表达式比您的更简单,只需匹配三个不包含“:“grep在服务器上完成,我可以将\0传输到客户端吗?”?我尝试过,但当前\0未出现在服务器的响应中。这应该是可能的。并非所有查看器都正确显示空字节。尝试通过十六进制查看器运行它,以确保正确查看输出。是的,它工作:)就像您所说的
    grep-nriIHZ
    /^(.*)0(\d+):(.*)$/
    完美。chrome的网络控制台不显示,但存在ASCII空字符。grep在服务器上完成,我可以将\0传输到客户端吗?我尝试过,但当前\0未出现在服务器的响应中。这应该是可能的。并非所有查看器都正确显示空字节。尝试通过十六进制查看器运行它,以确保正确查看输出。是的,它工作:)就像您所说的
    grep-nriIHZ
    /^(.*)0(\d+):(.*)$/
    完美。chrome的网络控制台不显示它,但存在ASCII空字符。