0

I swear i tried figuring this out myself all day, but my regex-foo is just not that good.

I'm trying to create a small parser function to convert strings with urls to html coded and tags

I know how complex a regex can be trying to figure out which urls to covert to what from a big string, so what I did is simply prefix the string to covert with a flag to tell the parser how to format it, and post fix it with the ";" char to tell the parser where that particular URL ends. This way the parser has lesser guest work to do resulting in easier to regex-match and faster for execution. I really dont need a generalize match and replace all.

So my formatting is as follows, where "X" is the url string:

  1. For URLs it will be url=X;
  2. For IMAGES it will be img=X;

so anything in between my prefix and post fix must be converted accordingly..

So for example, for images in my document, the string could be:

click this image img=http://example.com/image1.jpg;

and i need that converted to

click this image <a href="http://example.com/image1.jpg" target="_blank">
<img class="img img-responsive" src="http://example.com/image1.jpg"/></a>

I am able to do this easily in PHP buy preg_match() function

preg_match('/\img=(.+?)\;/i', $item_des, $matches)

here's the code block: enter image description here

I decided to push this routine to the browser instead of the backend (PHP) so i need similar or better JS solution.

Hoping anyone can help here, thanks!

BrownChiLD
  • 3,545
  • 9
  • 43
  • 61
  • Why not use document.exec https://developer.mozilla.org/en-US/docs/Web/API/document/execCommand .insertImage and contenteditable="true" https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content and than you only get document.getElementById('yours_contenteditable_el').innerHTML – Zydnar May 14 '17 at 09:46
  • 2
    Could you show us your best attempt? – Whothehellisthat May 14 '17 at 09:47

2 Answers2

1

try code below:

var str = "click this image img=http://example.com/image1.jpg;image2 img=http://example.com/image2.jpg;"
var phrases = str.split(';');
var totalRes = '';
phrases.forEach(function(str){
  totalRes += processPhrase(str);
});
console.log(totalRes);
function processPhrase(str) {
  var img = str.split('img=')
  var res = '';
  if (img.length > 1) { //img=X
    var url = img[1].replace(';', '');
    res = img[0] + "<a href='" + url + "' target='_blank'><img src='" + url + "'/></a>";
  } else {
    var url = str.split('url=');
    //Do for url=X here
  }
  console.info(res);
  return res;
}
Yashar Aliabbasi
  • 2,663
  • 1
  • 23
  • 35
  • tnx for this, have not tried it yet, but i wanted to know if i had a rather large text block , will this be process heavy compared to regex matching?? – BrownChiLD May 14 '17 at 10:45
1

You can use this regexp /(img|url)=(.+?);/g:

(img|url) : the type, should be grouped so we will know what to do with the value
=         : literal "="
(.+?)     : a number of characters (use the non-greedy ? so it will match as fewer as possible)
;         : literal ";"

Read more about non-greedy regexps here.


Example:

var str = "click this image img=http://i.imgur.com/3wY30O4.jpg?a=123&b=456; and check this URL url=http://google.com/;. Bye!";


// executer is an object that has functions that apply the changes for each type (you can modify the functions for your need)
var executer = {
  "url": function(e) {
    return '<a target="_blank" href="' + e + '">' + e + '</a>';
  },
  "img": function(e) {
    return '<a target="_blank" href="' + e + '"><img src="' + e + '"/></a>';
  }
}

var res = str.replace(/(img|url)=(.+?);/g, function(m, type, value) {
  return executer[type](value);    // executer[type] will be either executer.url or executer.img, then we pass the value to that function and return its returned value
});

console.log(res);
Community
  • 1
  • 1
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • 1
    I think this must suppress `;` in result – Yashar Aliabbasi May 14 '17 at 10:00
  • so i tried this and it seems to work fine until it catches an ampersand sing "&" so if i have http://i.imgur.com/3wY30O4.jpg?a=123&b=456; and i want to put brackets around it, i get [http://i.imgur.com/3wY30O4.jpg?a=123&]b=456; Works fine otherwise. any ideas? – BrownChiLD May 14 '17 at 10:44
  • here's the snippet i used http://i.imgur.com/PZ30HOw.jpg See my previous explanation about the issue. – BrownChiLD May 14 '17 at 10:47
  • @BrownChiLD I bet it has something to do with encoding, you'e using `.html` to retrieve the content, then `&` will actually be `&` (notice the `;` at the end). `console.log` the return value of `.html` to verify my assumption. – ibrahim mahrir May 14 '17 at 10:54
  • @ibrahimmahrir spot on , that's what it was... so i added a replace routine var cur_des_html = cur_des_html.replace(/&/g,"&"); and now it works awesome! thanks so much! – BrownChiLD May 14 '17 at 11:04