breadcrumbs - Simple Bread Crumb with regex -
i trying create simple bread crumb regex splitting given url:
input url in form - http://someurl.com:1000/sites/site1/pages/programs/programs1/programs2/somepage.aspx?a=b;c=d
output expected - here: sites>site1>pages>programs>programs1>programs2>somepage
and here code have used:
<script type="text/javascript"> var url ="http://someurl:1000/sites/site1/pages/programs/programs1/programs2/somepage.aspx?a=b;c=d" var newurl = url.replace(new regexp( "^(?:([^:/?#]+):)?(?://((?:(([^:@]*):?([^:@]*))?@)?([^:/?#]*)(?::(\\d*))?))?((((?:[^?#/]*/)*)([^?#]*))(?:\\?([^#]*))?(?:#(.*))?)", "g"), function( $0, $1, $2, $3, $4, $5, $6, $7, $8, $9){ var domain = $1+ "://" + $2; var href = domain; var urlstrippeddown = $9; //split folders/pages within path /sites/site1/pages/programs/programs1/programs2/somepage.aspx var pieces = urlstrippeddown.replace( new regexp (\\w+.aspx)|(\\w+), //form bread crumb anchor tags , text function ($0, $1, $2) { 0 = $0; var test = "<a href='" + href + urlstrippeddown.substring(0,urlstrippeddown.indexof($0)+$0.length) +"'>" + zero.replace('.aspx','') + "</a>"; return test; } ); return pieces; } ); //document.write("you here: " + newurl.replace(/\//g,'')); document.write("you here: " + newurl); </script>
problem faced: issue facing final output (newurl) has forward slash ("/") between each link, replace ">" seems impossible. not sure how "/" getting inserted, though not showing part of text when "anchor" tag formed.
the expected output is- here: sites>site1>pages>programs>programs1>programs2>somepage
whereas get- here: /sites/site1/pages/programs/programs1/programs2/somepage , unable replace "/" ">"
p.s: commented line of code - if replace here, replaces "/" inside anchor tags. "/" should replaced in text , not in href of anchor
is there flaw in regex? there better ways handle this?
thanks!
your first replace doesn't serve other purpose split string parts: domain , path, instead should consider using regexp.exec
on string , parts way (see snippet below).
your newurl
variable contains html, can take advantage of , use in regex. if replace /<a
> <a
should have string such: > sites > site1 > pages > programs > programs1 > programs2 > somepage
. , have remove first >
imagine don't want that.
your regex seems overly complex need first match, , second replace might better of doing using indexof('/')
, loop instead since you're not leveraging on regex matches.
below you'll find function make desired link chain arbitrary valid uri using similar approach initial solution. have result of tinkering code seems waste not post it.
var url = "http://someurl:1000/sites/site1/pages/programs/programs1/programs2/somepage.aspx?a=b;c=d"; function makebreadcrumb(url){ // split match , split url domain , path. var urlsplitter = new regexp("^(.*?://[^/]+?/)([^?#:@]*)", ""); var matcher; if (matcher = urlsplitter.exec(url)) { var domain = matcher[1]; // match every "folder" or "file" in uri , replace "breadcrumb link". return matcher[2].replace(new regexp("([^/]+)(?:\\.[^\\.]+$|/)","g"), function(match,dir,pos,string) { return (pos > 0 ? " > " : "") + "<a href=\"" + domain + string.substring(0,pos) + match + "\">" + dir + "</a>"; } ); } return ""; } document.write("you here: " + makebreadcrumb(url));
Comments
Post a Comment