user407079
user407079

Reputation: 169

Javascript replace a character with a space

I need to get the name of the page from a url. I did this way:

var filename = window.location.href.substr(window.location.href.lastIndexOf('/')+1)
// -> 'state.aspx' 

var statelookup = filename.substr(0, filename.lastIndexOf('.'))
// -> 'state'

Now for e.g, my statelookup has a value like New-York or North-Carolina, how do I replace hyphen with a space in between?

Upvotes: 17

Views: 33284

Answers (3)

FishBasketGordo
FishBasketGordo

Reputation: 23122

You would use String's replace method:

statelookup = statelookup.replace(/-/g, ' ');

API Reference here.

Upvotes: 4

Marc B
Marc B

Reputation: 360572

statelookup = statelookup.replace('-', ' ')

Upvotes: 1

Madara's Ghost
Madara's Ghost

Reputation: 174947

string.replace(/-/g,' ');

Will replace any occurences of - with in the string string.

Upvotes: 27

Related Questions