JavaScript正則表達式進階指南

摘要:正則表達式是程序員的必備技能,想不想多學幾招呢?javascript

圖片描述

本文用JavaScript的exec方法來測試正則表達式。java

例如,正則表達式/F.*g/會匹配「以F開頭,以g結尾的字符串」,所以能夠匹配"Hello, Fundebug!"中的Fundebug,exec方法會返回一個數組,其第一個元素爲所匹配的子字符串。程序員

/F.*g/.exec("Hello, Fundebug!")[0]
// 'Fundebug'

非貪婪匹配

默認狀況下,正則表達式的量詞*、+、?、{},都是進行貪婪匹配,即匹配儘量多的字符正則表達式

例如,正則表達式/.+s/匹配的是「以空格符結尾的字符串」,咱們用它來匹配蘋果公司創始人喬布斯在斯坦福大學演講的名言「You time is limited, so don’t waste it living someone else’s life.」:express

/.+\s/.exec("You time is limited, so don’t waste it living someone else’s life.")[0]
// 'You time is limited, so don’t waste it living someone else’s '

.能夠匹配任意字符,而+表示匹配1次或者屢次,且是貪婪的,所以/.+s/匹配到了最後一個空格符才結束。小程序

當咱們在量詞*、+、?、{}後面緊跟着一個?,就能夠實現非貪婪匹配,即匹配儘可能少的字符微信小程序

例如,正則表達式/.+?s/匹配到第一個空格符就會結束:數組

/.+?\s/.exec("You time is limited, so don’t waste it living someone else’s life.")[0]
// 'You '

正向確定查找

使用正則表達式x(?=y),能夠匹配'x'僅僅當'x'後面跟着'y'。這話有點繞,簡單地說,就是匹配後面是y的x,這裏的x和y都表明正則表達式。微信

例如,對於博客RabbitMQ入門教程的地址"https://blog.fundebug.com/2018/04/20/rabbitmq_tutorial/",若是須要匹配出域名fundebug的話,可使用/[a-z]+(?=.com)/,匹配「在.com前面的英文單詞」less

/[a-z]+(?=\.com)/.exec("https://blog.fundebug.com/2018/04/20/rabbitmq_tutorial/")[0]
// 'fundebug'

廣告:歡迎免費試用Fundebug,爲您監控線上代碼的BUG,提升用戶體驗~

正向否認查找

與正向確定查找所對應的是正向否認查找,使用正則表達式x(?!y),能夠"匹配'x'僅僅當'x'後面不跟着'y'"。

例如,小學生都知道的圓周率是3.1415926,不會的同窗能夠這樣記「山頂上有一座寺廟,寺廟裏面有一壺酒,還有一塊肉」。如何匹配小數點後面的數字呢?可使用/d+(?!\.)/,匹配"後面沒有小數點的數字":

/\d+(?!\.)/.exec("3.1415926")[0]
// '1415926'

而使用以前提到的正向確定查找,就能夠匹配小數點前面的數字:

/\d+(?=\.)/.exec("3.1415926")[0]
// '3'

多行匹配

下面是鮑勃·迪倫的《Forever Young》歌詞:

May God bless and keep you always,
may your wishes all come true,
may you always do for others
and let others do for you.
may you build a ladder to the stars
and climb on every rung,
may you stay forever young,
forever young, forever young,
May you stay forever young.

如何匹配以forever開頭的那句歌詞forever young, forever young呢?

這樣寫/^forever.+/是錯誤的:

/^forever.+/.exec("May God bless and keep you always,\nmay your wishes all come true,\nmay you always do for others\nand let others do for you.\nmay you build a ladder to the stars\nand climb on every rung,\nmay you stay forever young,\nforever young, forever young,\nMay you stay forever young.")
// null

爲何錯了?由於^匹配的整個字符串的開始,而是否是每一行的開始。

正則表達式指定m選項,便可支持多行匹配,這時^$匹配的是每一行的開始和結束,所以正確的正則表達式是/^forever.+/m

/^forever.+/m.exec("May God bless and keep you always,\nmay your wishes all come true,\nmay you always do for others\nand let others do for you.\nmay you build a ladder to the stars\nand climb on every rung,\nmay you stay forever young,\nforever young, forever young,\nMay you stay forever young.")[0]
// 'forever young, forever young,'

捕獲括號

在正則表達式中使用小括號(),能夠提取出字符串中的特定子串。

例如,Fundebug是在2016年雙11正式上線的,時間是"2016-11-11",如何提取其中的年、月、日呢?以下:

/(\d{4})-(\d{2})-(\d{2})/.exec("2016-11-11")
// [ '2016-11-11', '2016', '11', '11', index: 0, input: '2016-11-11' ]

可知,3個小括號中的正則表達式分別匹配的是年月日,其結果依次爲exec返回數組中的1到3號元素。

參考

關於Fundebug

Fundebug專一於JavaScript、微信小程序、微信小遊戲、支付寶小程序、React Native、Node.js和Java線上應用實時BUG監控。 自從2016年雙十一正式上線,Fundebug累計處理了10億+錯誤事件,付費客戶有Google、360、金山軟件、百姓網等衆多品牌企業。歡迎你們免費試用

版權聲明

轉載時請註明做者Fundebug以及本文地址:
https://blog.fundebug.com/2018/05/02/advanced_regular_expression/

相關文章
相關標籤/搜索