Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

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
0
wal_toor
Asked:
wal_toor
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now