Javascript – Function string SEO Friendly

Replace Space Special Character and Uppercase

replace some string which contain Space, Special Character like “%$#@!&*()^”
with dash ‘-‘

change to lowercase

remove first and last dash

/**
auth: g3n1k
mail: g3n1k@yahoo.com
time: 18-July-2013
*/
function gen_uri(txt_src){

 var output = txt_src.replace(/[^a-zA-Z0-9]/g,' ').replace(/\s+/g,"-").toLowerCase();
 /* remove first dash */
 if(output.charAt(0) == '-') output = output.substring(1);
 /* remove last dash */
 var last = output.length-1;
 if(output.charAt(last) == '-') output = output.substring(0, last);

 return output;
}

test: # // XX #$% DFRT dsdf X# become: xx-dfrt-dsdf-x

change title to url seo
change title to url seo

I use this function can use to make Friendly URL (SEO), see the url browser😉

https://g3n1k.wordpress.com/2013/07/18/javascript-replace-space-special-character-and-uppercase/

Published by

G3n1k

just to remember what i had known :)

2 thoughts on “Javascript – Function string SEO Friendly”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s