1

I'm trying to do a replace on a hash so I can call a html file that matches its name.

For example:

var url = '/partials/' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.html';

and hash will be:

#/home/

at the moment it will only remove the #

what have I missed?

2
  • I think your code just replaces the # with nothing so it is removed Commented Feb 12, 2014 at 15:55
  • Give an example input and output. Get more answers. Commented Feb 12, 2014 at 16:03

1 Answer 1

2

It looks like you also want to remove the leading and trailing / as well. Try using a pattern like this:

/^#\/|\/$/g

For example:

var hash = '#/home/';
var url = '/partials/' + ( hash.replace( /^#\/|\/$/g, '' ) || 'blank' ) + '.html';
console.log(url); // "/partials/home.html"

Or possibly try using match instead of replace, like this:

var hash = '#/home/';
var url = '/partials/' + ( hash.match(/\w[\w/]*\w/) || 'blank' ) + '.html';
console.log(url); // "/partials/home.html"
Sign up to request clarification or add additional context in comments.

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.