1

I'm using this code I stumbled upon to 'clean up' urls with jquery:

.replace(/[^\w ]+/g,'') // and
.replace(/ +/g,'-')

The first one removes non alpha-numeric characters, while the second turns spaces into a dash

My question is: Is there a way to exclude a character, in my situation, # , from this first .replace() ?

Thank you.

1
  • Note that .replace() is a standard JavaScript String method, not a jQuery thing. (I hope you don't mind, but I retagged your question accordingly.) Commented Jul 10, 2012 at 3:53

1 Answer 1

3
.replace(/[^\w#\/ ]+/g,'') // and
Sign up to request clarification or add additional context in comments.

5 Comments

Also do you know how I would do this with a / ?
THANK YOU!!! so I can keep adding on exceptions like that? This worked. Thank you very much for your help!
@iight: [^...] construction means "match everything except of what is specified in .... In your case [^\w#\/ ] - is "match everything except of \w (letters and underscore), # sign, / sign and a space"
Thank you for the deeper explanation! Do you know what I can search for to learn more about this?
Or MDN for more info about JS's implementation of regex.

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.