I am building a CMS where a user can customize the SEO URL of a page via a text input control. For example, say the user is creating a gallery, and they want their page to be accessed at http://www.example.com/my-1st-gallery
.
Notice how the "my-1st-gallery" portion doesn't contain any illegal characters for a URL. Since most users won't know what is allowed and not allowed, I'd like to create a JavaScript regex filter which can filter/convert all illegal characters on key-up.
I know how to use jQuery/JavaScript to listen for key-up events, but I'm not sure how to use a regex to do the following:
- Filter all characters other than a-z, A-Z, 0-9, a "-", a "_", and a space.
- Change any "_" and spaces to a "-", and let the user know that the given character has been converted into "-".
Could someone provide a good example of how to do the regex part. Again, I understand how to listen for key-up events.
Ok, with all of these good answers, I think I can piece this together for my web app. I wish I could select more than one answer as my final! :S Thank you all!