Search code examples
javascriptregexmarkdowncodemirror

change marked.js image/link expression


I need to change marked.js image expression.

The original expression is: /^\!?\[(inside)\]\(href\)(\w*\S*)*/ ---> ![]()

I need to get id of all images, so I need to transform the expression to get id value from this ----> {>"id"<}![inside](href).

I tryed to change expression to something like this: /^\{\>index\>\}\!?\[(inside)\]\(href\)(\w*\S*)*/

And after thar I need to write an expression to get the "index" value of {>index<}![inside](href)

Examples for inside & 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)
();

Anyone can help me to build this expression?


Solution

  • If I get it right, you want to obtain some data from a link that has some specific format, e.g. {>"id"<}![inside](href)and_the_rest.

    So, the best regex I can suggest for the moment can look like {>([^<]*)<}\!?\[([^]]+)\]\(([^)]*)\)(\w*\S*)*. It assumes you have "id" inside > and <, "inside" is inside [ and ], and "href" is inside ( and ) one right after another.

    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'/>