javascript - Most efficient way of converting plain text to HTML, Match or Regexp -


i have large text document filled random words, urls, email-addresses etc. example: "word 2014 john@doe.com http://www.example.com/ http://example.com/image.gif", differently, there linebreaks, multiple spaces, tabs etc. , data fast become huge (it type of bookmarking service data arriving time in form of images, text , hyperlinks).

another example of content in text document (the 1 use testing):

http://movpod.in/images3/movpod-logo.png https://dt8kf6553cww8.cloudfront.net/static/images/developers/chooser-drawing-vfln1ftk6.png http://xregexp.com/assets/regex_cookbook.gif asd asd ad feaf apa http 

i want wrap these strings in tags, , able target out images, hyperlinks, emails , strings. have tried different ways unsure best, , also, there regexp not understand.

the end result should be:

<span>word</span> <span>2014</span>  <a class="mail" href="mailto:john@doe">john@doe.com</a>  <a class="url" href="http://www.example.com/">http://www.google.com/</a>  <a class="img" href="http://example.com/image.gif">http://example.com/image.gif</a>" 

match. approach not keeping text order intact, works.

arr = data.split("\n"); (i = 0; < arr.length; i++) {     arr2 = arr[i].split(' ');     (j = 0; j < arr2.length; j++)     {         if (arr2[j].match(/(.gif|.png|.jpg|.jpeg)/))         {             ext = arr2[j].substr(-4);             ext = ext.replace(".","");             imgs += '<a class="img '+ext+'" href="'+arr2[j]+'">'+arr2[j]+'</a>';         }         else if (arr2[j].match(/(http:)/))         {             urls += '<a class="url" href="'+arr2[j]+'">'+arr2[j]+'</a>';         }         else         {             spans += '<span>'+arr2[j]+'</span>';         }     } } 

regexp. thought possible inverse @ exp_all, in else containing http. not however.

var exp_img     = /(https?:\/\/([\s]+?)\.(jpg|jpeg|png|gif))/g,     exp_link    = /([^"])(https?:\/\/([a-z-\.]+)+([a-z]{2,4})([\/\w-_]+)\/?)/g,     exp_all     = /^((?!http).)*$/g;      text        = data.replace(exp_all, '<span>$3</span>');     text        = text.replace(exp_img, '<a class="img" href="$1">$1</a>');     text        = text.replace(exp_link, '<a class="url" href="$2">$2</a>'); 

so, best way of accomplishing plain-text html conversion appreciated. love if there type of library this. looking @ markdown still have update plain-text markdown, guess not option.

and if possible strip out "http://" , have clean , neat possible.

im making few assumptions data (for example, every entry there.) if that's true, should work fine:

    <script>      var data = ['word\n 2014\t\t    john@doe.com\n\n\n\n\n http://www.example.com/ http://example.com/image.gif apa http',                 'fooo 2013 foo@bar.com http://www.blah.com/ http://blah.com/gif.gif asd asd ad feaf'];      function htmlify(string){         var elem = string.replace(/[^\w\s\/@:\.]/g,'').replace(/\s+/g, ' ').split(' ');         var result = [];         (var = 0; < elem.length; i++){             if (elem[i].match(/http:/)) {                 if (elem[i].substr(-4).match(/.gif|.png|.jpg|.jpeg/)){                     result.push("<a class='img' href='" + elem[i] + "'>" + elem[i] + "</a>");                 } else {                     result.push( "<a class='url' href='" + elem[i] + "'>" + elem[i] + "</a>");                 }             } else if (elem[i].match(/\w+@\w+\.\w+/)){                     result.push("<a class='mail' href='mailto:" + elem[i] + "'>" + elem[i] + "</a>");             } else {                 result.push("<span>" + elem[i] + "</span>");             }         }         return result;     }      var result = data.map(htmlify);     console.log(result);      </script> 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -