17

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?

3 Answers 3

27

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

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

Sign up to request clarification or add additional context in comments.

Comments

4

You would use String's replace method:

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

API Reference here.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.