V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
spencerqiu

请问匹配全文中所有两个 HTML 标签之间内容的 grep 正则表达式 怎么写?

  •  
  •   spencerqiu · Jun 14, 2018 · 3066 views
    This topic created in 2874 days ago, the information mentioned may be changed or developed.
    >cat sample.txt
    </span>
                                    <span _content="">
    那种写书不好好解释
    动不动就留作习题
    
    <div content="" class="sample" style="sample: 5px;">
    
    </span>
                                    <span _content="">
    课后习题的答案除了最终结果什么都没有的作者
    我真的很想踢爆你的头了……​​​
    
    <div content="" class="sample" style="sample: 5px;">
    
    期望匹配出:
    那种写书不好好解释
    动不动就留作习题
    
    课后习题的答案除了最终结果什么都没有的作者
    我真的很想踢爆你的头了……
    
    */即起始标签为带换行的两个标签</span>
                                    <span _content="">
    结束为 <div content="" class="sample" style="sample: 5px;">
    */
    

    网上搜了一大堆都不管用,要么根本不行,要么换行不行 = =

    9 replies    2018-06-14 11:30:24 +08:00
    grewer
        1
    grewer  
       Jun 14, 2018
    var str = '</span> <span _content="">那种写书不好好解释动不动就留作习题<div content="" class="sample" style="sample: 5px;"></span> <span _content="">课后习题的答案除了最终结果什么都没有的作者我真的很想踢爆你的头了……​​​<div content="" class="sample" style="sample: 5px;">'
    str.match(/>.*?</g)
    spencerqiu
        2
    spencerqiu  
    OP
       Jun 14, 2018
    @grewer
    是一整个 HTML,所以还有很多其他标签 = =
    grewer
        3
    grewer  
       Jun 14, 2018
    @spencerqiu 组件了解一下正则,然后写一个,不然想要契合你的需求是很难的
    BOYPT
        4
    BOYPT  
       Jun 14, 2018
    收费定制正则表达式
    feibinyang
        5
    feibinyang  
       Jun 14, 2018
    需求是获取所有文本节点吧,这用 DOM 操作遍历更方便吧
    wplct
        6
    wplct  
       Jun 14, 2018
    bs4 了解一下
    rabbbit
        7
    rabbbit  
       Jun 14, 2018
    另一种思路,不去匹配文字,把 html 符号全替换掉
    https://imgur.com/a/sgT5mIJ
    rabbbit
        8
    rabbbit  
       Jun 14, 2018
    Alife
        9
    Alife  
       Jun 14, 2018 via iPhone
    正则前将其变成一行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5636 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 08:24 · PVG 16:24 · LAX 01:24 · JFK 04:24
    ♥ Do have faith in what you're doing.