Javascript replaces character with space

I need to get the page name from the url. I did like this:

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

Now, for example, my statelookup matters as much as New York or North Carolina, how do I replace a hyphen with a space between them?

+7
source share
3 answers

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

replace any occurrences - with in the string string .

+12
source

You would use the String replace method:

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

API link here.

+2
source
 statelookup = statelookup.replace('-', ' ') 
0
source

All Articles