propcode
propcode

Reputation: 309

Capitalize first letter of a part of an href

I have the following kind of link:

http://myexampple.com/gallery/overview/portrait#

Where the word portrait changes, it is dynamic depending on custom post. I need to capitalize the first letter of it so I get:

http://myexampple.com/gallery/overview/Portrait#

any idea how to do it?

Upvotes: 0

Views: 214

Answers (2)

mahmoud nezar sarhan
mahmoud nezar sarhan

Reputation: 756

you can define a function called replaceAt (index , with )

I suggest :

String.prototype.replaceAt=function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
}

and now you can call it like this :

var link = $("link-id").text() ;
var index = link.lastIndexOf('/') + 1 ;
link = link.replaceAt (index , index.toUpperCase() ) ;
$("link-id").html(link) ;

Upvotes: 0

epascarello
epascarello

Reputation: 207527

You can use string replace with a regular expression

var str = "http://myexampple.com/gallery/overview/portrait#"
var updated = str.replace(/(\/.)([^\/]+[#$])/, function(full, f, s){ return f.toUpperCase() + s;});

Upvotes: 2

Related Questions