代码之家  ›  专栏  ›  技术社区  ›  Attila O.

将单行JavaScript注释(//)与re匹配

  •  6
  • Attila O.  · 技术社区  · 16 年前

    我想使用python的 re 模块。例如:

    // this is a comment
    var x = 2 // and this is a comment too
    var url = "http://www.google.com/" // and "this" too
    url += 'but // this is not a comment' // however this one is
    url += 'this "is not a comment' + " and ' neither is this " // only this
    

    编辑1 :

    foo = 'http://stackoverflow.com/' // these // are // comments // too //
    

    编辑2 :

    bar = 'http://no.comments.com/'
    
    2 回复  |  直到 16 年前
        1
  •  7
  •   Fabio Zadrozny    13 年前

    我的regex权限有点过时了,所以我用你的问题刷新了我的记忆。 它变成了一个相当大的正则表达式,主要是因为我还想过滤多行注释。

    import re
    
    reexpr = r"""
        (                           # Capture code
            "(?:\\.|[^"\\])*"       # String literal
            |
            '(?:\\.|[^'\\])*'       # String literal
            |
            (?:[^/\n"']|/[^/*\n"'])+ # Any code besides newlines or string literals
            |
            \n                      # Newline
        )|
        (/\*  (?:[^*]|\*[^/])*   \*/)        # Multi-line comment
        |
        (?://(.*)$)                 # Comment
        $"""
    rx = re.compile(reexpr, re.VERBOSE + re.MULTILINE)
    

    下面是一个如何提取这些信息的示例。

    code = r"""// this is a comment
    var x = 2 * 4 // and this is a comment too
    var url = "http://www.google.com/" // and "this" too
    url += 'but // this is not a comment' // however this one is
    url += 'this "is not a comment' + " and ' neither is this " // only this
    
    bar = 'http://no.comments.com/' // these // are // comments
    bar = 'text // string \' no // more //\\' // comments
    bar = 'http://no.comments.com/'
    bar = /var/ // comment
    
    /* comment 1 */
    bar = open() /* comment 2 */
    bar = open() /* comment 2b */// another comment
    bar = open( /* comment 3 */ file) // another comment 
    """
    
    parts = rx.findall(code)
    print '*' * 80, '\nCode:\n\n', '\n'.join([x[0] for x in parts if x[0].strip()])
    print '*' * 80, '\nMulti line comments:\n\n', '\n'.join([x[1] for x in parts if x[1].strip()])
    print '*' * 80, '\nOne line comments:\n\n', '\n'.join([x[2] for x in parts if x[2].strip()])
    
        2
  •  1
  •   Seth    16 年前

    在任何情况下,这都是有效的:

    import re
    
    rx = re.compile(r'.*(//(.*))$')
    
    lines = ["// this is a comment", 
        "var x = 2 // and this is a comment too",
        """var url = "http://www.google.com/" // and "this" too""",
        """url += 'but // this is not a comment' // however this one is""",
        """url += 'this "is not a comment' + " and ' neither is this " // only this""",]
    
    for line in lines: 
        print rx.match(line).groups()
    

    上述各项的产出:

    ('// this is a comment', ' this is a comment')
    ('// and this is a comment too', ' and this is a comment too')
    ('// and "this" too', ' and "this" too')
    ('// however this one is', ' however this one is')
    ('// only this', ' only this')
    

    我不确定你在用javascript做什么 之后 JSMin 也许会有帮助。无论如何,它删除注释的效果足够好,并且有一个 implementation in python .