Jquery Slug function

Hello all,

I have a function that creates a slug from a string. Works great! But is it possible to have a set of characters that are allowed here?

The function I use:

function convertToSlug(Text)
{
    return Text
        .toLowerCase()
        .replace(/[^\w ]+/g,'')
        .replace(/ +/g,'-')
        ;
}

Open in new window


How can I alter this so the function allowes a '/' (backslash)?

Thanks!

greetz,
walter
LVL 8
wal_toorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
function convertToSlug(Text)
{
    return Text
        .toLowerCase()
        .replace(/[^\w ]+/g,'')
        .replace(/ +/g,'-')
        .replace(/ + /g,'/')        ;
0
wal_toorAuthor Commented:
Hi plusone,

Hmm does'nt seem to work, a string like '2/Corporate Videos/20/MINI - Making of Monster' still gets returnd as '2corporate-videos20mini-making-of-monster' And I need the backslashes.

Thankz!

greetz,
walter
0
plusone3055Commented:
function convertToSlug(Text)
{
    return Text
        .toLowerCase()
        .replace(/[^\w ]+/g,'/')
        .replace(/ + /g,'/')  

'2/Corporate Videos/20/MINI - Making of Monster'
'2corporate-videos20mini-making-of-monster'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wal_toorAuthor Commented:
Hello plusone,

Thank you, this keeps the backslashes, but does not replace the spaces (to '-').

Check: http://jsfiddle.net/dpQPH/

Greetz,
walter
0
wal_toorAuthor Commented:
Helped me on the way, but did'nt receive any comment after last post
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.