There is a text line like
var text = "somthing text and habrahabr.ru/login.php?vk=1 link " Link taken from the baldy. It is necessary in any way to make the replacement of this line with a link And not only of this kind.
What else should be considered:
- support for these protocols (i.e. conversion to a link): http: //, https: //, ftp: //, skype: //, steam: //
- any domain names (such as .рф) and support for subdomains proper
- Cyrillic domain name support
All this is wrapped in a function, for example. Already written and partially working:
function findAndReplaceLink(inputText) { var url = "http://test.com" var pattern = /([-a-zA-Z0-9@:%_\+.~#?&\/\/=]{2,256}\.[a-zA-Zа-яА-ЯёЁ]{2,4}\b(\/?[-a-zA-Z0-9а-яА-ЯёЁ@:%_\+.~#?&\/\/=]*)?)/gi; var replacedText = inputText.replace(pattern, '<a href="'+url+'/go.php?to=$1" target="_blank">$1</a>'); return replacedText; } The output should be the line:
"somthing text and <a href='http://test.com/go.php?to=http://habrahabr.ru/login.php?vk=1' target='_blank'>habrahabr.ru/login.php?vk=1</a> link " Help, please, finish the functionality. Thanks in advance.
UPDATE. I found a code with a similar functionality (I took it from vk.com), but I don’t know how to change it for my needs, for my function. will not help?
message = (message || '').replace(/(^|[^A-Za-z0-9А-Яа-яёЁ\-\_])(https?:\/\/)?((?:[A-Za-z\$0-9А-Яа-яёЁ](?:[A-Za-z\$0-9\-\_А-Яа-яёЁ]*[A-Za-z\$0-9А-Яа-яёЁ])?\.){1,5}[A-Za-z\$рфуконлайнстРФУКОНЛАЙНСТ\-\d]{2,22}(?::\d{2,5})?)((?:\/(?:(?:\&|\!|,[_%]|[A-Za-z0-9А-Яа-яёЁ\-\_#%?+\/\$.~=;:]+|\[[A-Za-z0-9А-Яа-яёЁ\-\_#%?+\/\$.,~=;:]*\]|\([A-Za-z0-9А-Яа-яёЁ\-\_#%?+\/\$.,~=;:]*\))*(?:,[_%]|[A-Za-z0-9А-Яа-яёЁ\-\_#%?+\/\$.~=;:]*[A-Za-z0-9А-Яа-яёЁ\_#%?+\/\$~=]|\[[A-Za-z0-9А-Яа-яёЁ\-\_#%?+\/\$.,~=;:]*\]|\([A-Za-z0-9А-Яа-яёЁ\-\_#%?+\/\$.,~=;:]*\)))?)?)/ig, function () { // copied to notifier.js:3401 var matches = Array.prototype.slice.apply(arguments), prefix = matches[1] || '', protocol = matches[2] || 'http://', domain = matches[3] || '', url = domain + (matches[4] || ''), full = (matches[2] || '') + matches[3] + matches[4]; if (domain.indexOf('.') == -1 || domain.indexOf('..') != -1) return matches[0]; var topDomain = domain.split('.').pop(); if (topDomain.length > 6 || indexOf('info,name,aero,arpa,coop,museum,mobi,travel,xxx,asia,biz,com,net,org,gov,mil,edu,int,tel,ac,ad,ae,af,ag,ai,al,am,an,ao,aq,ar,as,at,au,aw,ax,az,ba,bb,bd,be,bf,bg,bh,bi,bj,bm,bn,bo,br,bs,bt,bv,bw,by,bz,ca,cc,cd,cf,cg,ch,ci,ck,cl,cm,cn,co,cr,cu,cv,cx,cy,cz,de,dj,dk,dm,do,dz,ec,ee,eg,eh,er,es,et,eu,fi,fj,fk,fm,fo,fr,ga,gd,ge,gf,gg,gh,gi,gl,gm,gn,gp,gq,gr,gs,gt,gu,gw,gy,hk,hm,hn,hr,ht,hu,id,ie,il,im,in,io,iq,ir,is,it,je,jm,jo,jp,ke,kg,kh,ki,km,kn,kp,kr,kw,ky,kz,la,lb,lc,li,lk,lr,ls,lt,lu,lv,ly,ma,mc,md,me,mg,mh,mk,ml,mm,mn,mo,mp,mq,mr,ms,mt,mu,mv,mw,mx,my,mz,na,nc,ne,nf,ng,ni,nl,no,np,nr,nu,nz,om,pa,pe,pf,pg,ph,pk,pl,pm,pn,pr,ps,pt,pw,py,qa,re,ro,ru,rs,rw,sa,sb,sc,sd,se,sg,sh,si,sj,sk,sl,sm,sn,so,sr,ss,st,su,sv,sx,sy,sz,tc,td,tf,tg,th,tj,tk,tl,tm,tn,to,tp,tr,tt,tv,tw,tz,ua,ug,uk,um,us,uy,uz,va,vc,ve,vg,vi,vn,vu,wf,ws,ye,yt,yu,za,zm,zw,рф,укр,сайт,онлайн,срб,cat,pro,local'.split(','), topDomain) == -1) { if (!/^[a-zA-Z]+$/.test(topDomain) || !matches[2]) { return matches[0]; } } if (matches[0].indexOf('@') != -1) { return matches[0]; } try { full = decodeURIComponent(full); } catch (e){} if (full.length > 55) { full = full.substr(0, 53) + '..'; } full = clean(full).replace(/&/g, '&'); if (!susp && domain.match(/^([a-zA-Z0-9\.\_\-]+\.)?(vkontakte\.ru|vk\.com|vkadre\.ru|vshtate\.ru|userapi\.com|vk\.me)$/)) { url = replaceEntities(url).replace(/([^a-zA-Z0-9#%;_\-.\/?&=\[\]])/g, encodeURIComponent); var tryUrl = url, hashPos = url.indexOf('#/'), mtch, oncl = ''; if (hashPos >= 0) { tryUrl = url.substr(hashPos + 1); } else { hashPos = url.indexOf('#!'); if (hashPos >= 0) { tryUrl = '/' + url.substr(hashPos + 2).replace(/^\//, ''); } } mtch = tryUrl.match(/^(?:https?:\/\/)?(?:vk\.com|vkontakte\.ru)?\/([a-zA-Z0-9\._]+)\??$/); if (mtch) { if (mtch[1].length < 32) { oncl = ' mention_id="' + mtch[1] + '" onclick="return mentionClick(this, event)" onmouseover="mentionOver(this)"'; } } return prefix + '<a href="'+ (protocol + url).replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>') + '" target="_blank"' + oncl + '>' + full + '</a>'; } return prefix + '<a href="away.php?utf=1&to=' + encodeURIComponent(protocol + replaceEntities(url)) + '" target="_blank" onclick="return goAway(\''+ clean(protocol + url) + '\', {}, event);">' + full + '</a>'; });