2015-04-05 128 views
0

我需要更改marked.js圖像表達式。更改marked.js圖像/鏈接表達式

原始表達式爲:/^\!?\[(inside)\]\(href\)(\w*\S*)*/ --->![]()

我需要獲得所有圖像的ID,所以我需要改造表達式中獲取價值,從這個---->{>"id"<}![inside](href) ID。

我tryed改變表達是這樣的:/^\{\>index\>\}\!?\[(inside)\]\(href\)(\w*\S*)*/

而且塔爾後,我需要寫一個表達式得到的{>index<}![inside](href)

示例「指數」值內& HREF:

inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/; 
inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/; 
inline._index = /(?:^\{\>[^\<\}]*\<\}|[^\{\>\<\}]|\<\}(?=[^\{\>]*\<\}))/; // I tryed this, but not works 

//replacing 

inline.link = replace(inline.link) 
('inside', inline._inside) 
('href', inline._href) 
('index', inline._index) 
(); 

任何人都可以幫助我建立這種表達?

回答

1

如果我理解正確,您希望從具有某種特定格式的鏈接獲取一些數據,例如{>"id"<}![inside](href)and_the_rest

因此,目前的best regex I can suggest可能看起來像{>([^<]*)<}\!?\[([^]]+)\]\(([^)]*)\)(\w*\S*)*。假設你在><之內有「id」,「inside」在[]之內,「href」在()之內。

function FindValues(str) { 
 
    var re = /\{\>([^<]*)\<\}\!?\[([^\]]+)\]\(([^\)]*)\)(\w*\S*)*/; 
 
    var m; 
 

 
    if ((m = re.exec(str)) !== null) 
 
    { 
 
    return [m[1], m[2], m[3], m[4]]; 
 
    } 
 
} 
 
    
 
var vls = FindValues('{>"id"<}![inside](href)and_the_rest'); 
 
document.getElementById('id').innerHTML = "ID: " +vls[0] + "<br>INSIDE: " + vls[1] + "<br>HREF: " + vls[2] + "<br>REST: " + vls[3];
<div id='id'/>

+0

暈! 非常感謝你,它是100%正確的! – 2015-04-05 23:13:01

+0

你好,我只有一個問題,鏈接的[文本](http://example.com)'。 字符串示例: 原始正則表達式返回給我:'[「[text](http://example.com)」,「text」,「http://example.com」,未定義,索引:0,輸入:「[文本](http://example.com)和」]'之後。 使用該正則表達式.exec terurns:'[「[text](http://example.com)」,undefined,undefined,「text」,「http://example.com」,undefined,index:17,輸入:「在[text](http://example.com)之前和之後的一些文本]' 我需要在沒有」之前的一些文本「的情況下得到」輸入「,您能幫我解決嗎? – 2015-04-06 09:33:08

+0

你能提供一個確切的輸入字符串嗎?我沒有在'[text](http://example.com)'中看到「輸入」,也沒有看到「之前的一些文字」。 – 2015-04-06 09:40:08