选择以一个单词开头的行(javascript)

时间:2016-11-29 18:25:12

标签: javascript regex

我想构建两个选择的正则表达式:

  1. 以'religion'开头的行。我试试/^religion/g但是我     没有结果

  2. 匹配日期和内容。例如:867.1.1 = {b_reykjavik = tb_shipyard_1} 我尝试\d{3}\.([1-12])\.([1-31]) = {这会选择867.1.1 = {。 现在,如何选择其他字符直到}?示例:https://www.debuggex.com/r/GloZ8dZDVLJ5jc29

  3. 案文是:

    # 1 - Vestisland
    b_reykjavik = tribal    
    #b_alftanes = city
    #b_skalholt = temple
    #b_pingvellir = castle
    #b_kjalarnes = castle
    #b_hvamm = castle
    #b_hlidarendi = castle
    #b_borg = castle    
    # Misc
    culture = norse
    religion = zoroastrian
    terrain = arctic
    
    # History
    867.1.1 = {
        b_reykjavik = tb_shipyard_1
    }
    900.1.1 = {
        b_reykjavik = castle
        b_reykjavik = ca_shipyard_1
        b_alftanes = city
    }
    1000.1.1 = {
        culture = norwegian
        religion = zoroastrian = sunni = catholic
    }
    

1 个答案:

答案 0 :(得分:1)

您可以使用

/^(?:religion.*|\d+(?:\.\d+){2} = {[^}]*})/gm

请参阅Debuggex Demo

<强>详情:

  • ^ - 一行的开头(因为m修饰符使^在一行的开头匹配)
  • (?:religion.* - 备选方案1:religion后跟除了换行符之外的任何0 +字符
  • | - 或
  • \d+(?:\.\d+){2} = {[^}]*}) - 1位数字,后跟2个. +一个或多个数字的序列(请参阅\d+(?:\.\d+){2}),然后是空格,=,空格,{ {1}},除{之外的零个或多个字符(请参阅})和[^}]*

要仅抓取部分记录,请在这些子图案周围使用捕获组(}),请参阅following regex

(...)

&#13;
&#13;
/^(?:religion = (.*)|\d+(?:\.\d+){2} = {([^}]*)})/gm
                ^  ^                    ^     ^
&#13;
&#13;
&#13;