2016-11-22 123 views
-6

假設我有以下文字如何使用正則表達式提取字符串?

for (;;);{"__ar":1,"__sf":"k","payload":null,"domops":[["appendContent","^div.fbProfileBrowserListContainer",true,{"__html":"\u003Cdiv class=\"fbProfileBrowserList expandedList\" id=\"100008123852509\">\u003Cul class=\"uiList clearfix _5bbv _4kg _704 _4ks\">\u003Cli class=\"fbProfileBrowserListItem\">\u003Cdiv class=\"clearfix _5qo4\">\u003Ca class=\"_8o _8t lfloat _ohe\" href=\"https:\/\/www.facebook.com\/tasvirmanepal\/?fref=pb\" tabindex=\"-1\" aria-hidden=\"true\">\u003Cimg class=\"_s0 _rw img\" src=\"https:\/\/fb-s-b-a.akamaihd.net\/h-ak-xta1\/v\/t1.0-1\/c13.0.50.50\/p50x50\/12246747_952307471472706_3389977056619055535_n.jpg?oh=fdcd99bd098ad7d60b67701358bdbc97&oe=58D45D87&__gda__=1489984135_7befa40c475cf7f2a6aa021e97d7f429\" alt=\"\" \/>\u003C\/a>\u003Cdiv class=\"clearfix _42ef\">\u003Cdiv class=\"_6a rfloat _ohf\">\u003Cdiv class=\"_6a _6b\" style=\"height:50px\">\u003C\/div>\u003Cdiv class=\"_6a _6b\">\u003Cdiv class=\"_5t4x\">\u003Cspan

現在,我想提取字符串如

src=\"https:\/\/fb-s-b-a.akamaihd.net\/h-ak-xta1\/v\/t1.0-1\/c13.0.50.50\/p50x50\/12246747_952307471472706_3389977056619055535_n.jpg?oh=fdcd99bd098ad7d60b67701358bdbc97&oe=58D45D87&__gda__=1489984135_7befa40c475cf7f2a6aa021e97d7f429\"

+1

請分享你有這麼遠,爲什麼它不提供所期望的結果的代碼。 – wdosanjos

回答

0

一個正則表達式的工作原理與邏輯,先定義邏輯,然後寫正則表達式。

說:

  • 這場比賽是雙引號之間,並且犯規包含雙引號?
  • 比賽由src=\"

一個網站就像http://regexr.com/,你可以很方便地測試正則表達式的前面。

這是一個可能的正則表達式:src=\\\"[^"]*"但它不會幫助你與角落案件。

它基本上匹配以src\"開頭的所有內容,然後除了雙引號"之外的任何(零個或多個)字符,然後還包括雙引號。

0

請試試這個:

const regex = /src=\\"[^"]*?\\"/g; 
 
const str = `for (;;);{"__ar":1,"__sf":"k","payload":null,"domops":[["appendContent","^div.fbProfileBrowserListContainer",true,{"__html":"\\u003Cdiv class=\\"fbProfileBrowserList expandedList\\" id=\\"100008123852509\\">\\u003Cul class=\\"uiList clearfix _5bbv _4kg _704 _4ks\\">\\u003Cli class=\\"fbProfileBrowserListItem\\">\\u003Cdiv class=\\"clearfix _5qo4\\">\\u003Ca class=\\"_8o _8t lfloat _ohe\\" href=\\"https:\\/\\/www.facebook.com\\/tasvirmanepal\\/?fref=pb\\" tabindex=\\"-1\\" aria-hidden=\\"true\\">\\u003Cimg class=\\"_s0 _rw img\\" src=\\"https:\\/\\/fb-s-b-a.akamaihd.net\\/h-ak-xta1\\/v\\/t1.0-1\\/c13.0.50.50\\/p50x50\\/12246747_952307471472706_3389977056619055535_n.jpg?oh=fdcd99bd098ad7d60b67701358bdbc97&oe=58D45D87&__gda__=1489984135_7befa40c475cf7f2a6aa021e97d7f429\\" alt=\\"\\" \\/>\\u003C\\/a>\\u003Cdiv class=\\"clearfix _42ef\\">\\u003Cdiv class=\\"_6a rfloat _ohf\\">\\u003Cdiv class=\\"_6a _6b\\" style=\\"height:50px\\">\\u003C\\/div>\\u003Cdiv class=\\"_6a _6b\\">\\u003Cdiv class=\\"_5t4x\\">\\u003Cspan`; 
 
let m; 
 

 
while ((m = regex.exec(str)) !== null) { 
 
    // This is necessary to avoid infinite loops with zero-width matches 
 
    if (m.index === regex.lastIndex) { 
 
     regex.lastIndex++; 
 
    } 
 
    
 
    // The result can be accessed through the `m`-variable. 
 
    m.forEach((match, groupIndex) => { 
 
     console.log(`Found match, group ${groupIndex}: ${match}`); 
 
    }); 
 
}

相關問題