Giter VIP home page Giter VIP logo

living-story's Introduction

Living-Story,Thank Github

living-story's People

Contributors

geekxcan avatar

Watchers

James Cloos avatar  avatar

living-story's Issues

正则表达式中的向后匹配

16053648-bd6a-11e2-9d1f-a291aeb01b23

正则表达式可以写出复杂的匹配规则,但涉及到反选等需求时,往往不那么得心应手。
比如下面这段文本:
var s =
'  第一集\n' +
'  第一章 始动\n' +
'  “小卫啊,上工了。”工头老李看着一个……\n' +
'  第二章 突破\n' +
'  连续一个星期了,陈卫还是每天……\n' +
'  但让陈卫纳闷不已的是,他现在每天……\n' +
'  第三章 融合\n' +
'  午夜时分,天上挂着一轮明月,……\n'
想提取其章节信息:
第一集
第一章 始动
第二章 突破
第三章 融合

可以很容易写出匹配章节信息的正则表达式:
var m = s.match(/^\s_第.+[章集]._$/mg)
得到的结果是:
[
"  第一集",
"  第一章 始动",
"  第二章 突破",
"  第三章 融合"
]

现在问题反过来,如果我们想提取非章节信息,应该怎么办呢?
目前最直接的做法是,依旧是像上面这样,先得到章节信息,然后逐行替换为空,剩下的自然就是非章节信息了。
有没有办法,直接用正则匹配出非章节信息呢?
这就涉及正则的 Lookaround 问题。在未来的 JS 引擎里,我们可以这样写:
(?<=^|(第.+[章集])).*?(?=$|(第.+[章集]))

上面的正则,将会直接匹配出非章节文本,一步就能满足需求。
Lookaround 是 Lookahead 和 Lookbehind 的统称。对于向前匹配(Lookahead)相信大家都有所接触,包括向前正向匹配(Positive Lookahead)和向前负向匹配(Negative Lookahead),语法是 ?= 和 ?! 。上面的 (?=$|(第.+[章集]) 就是向前正向匹配,表示要匹配的字符后面必须是行尾($)或者是章节标题(第.+[章集])。
类似的,理解了向前匹配,向后匹配(Lookbehind)就很容易理解了。(?<=^|(第.+[章集])) 表示的含义是要匹配的字符前面必须是行首(^)或者是章节标题(第.+[章集])。
中间的 .*? 是非贪婪匹配任意字符。结合上面的 Lookaround,在此表达的含义就是匹配所有非章节信息。
注意:几年前,如果你像我一样喜欢看文本小说的话,上面的正则稍加变化,可以用来干不少事情。当时还在用 Windows,我最喜欢 EmEditor,对正则的支持非常完备。使用 EmEditor 的搜索替换,匹配上无敌的正则,可以很方便提取章节信息、以及去除空白行、将章节缩进重新排版等等。
Lookaround 在正则中非常有用,经常可以利用它来完成一些看似不可完成的匹配,合理利用 Lookaround 能让正则如虎添翼。
Lookaround 参考教程:http://www.regular-expressions.info/lookaround.html
当然,在 JavaScript 中你目前只能使用 Lookahead,还无法使用 Lookbehind。期待 ES6 的迅速实现与普及。
文 / 玉伯

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.