javascriptjqueryreplaceslugsanitization

Translate PHP slugifying function to jQuery


I'm moving an old PHP function to jquery. How can I convert his PHP function a jQuery function/script?

function superClean($str){
    $str = strtolower($str);
    $str = str_replace(" ", "-", $str);
    $str = preg_replace('/[^a-z0-9_]/i', '-', $str);
    $str = preg_replace('/_[_]*/i', '-', $str);
    $str = str_replace('---', '-', $str);
    $str = str_replace('--', '-', $str);
    $str = str_replace('-s-', 's-', $str);
    return $str;
}

Solution

  • wow - never used them that much but think they're gonna be really useful for the future!

    var String = "My'New ?! 'æ î Hat";
    var string = String.toLowerCase();
    string = string.replace(/[^a-zA-Z0-9_]/g,'-');
    alert(string.replace(/[-]{2,}/,'-'))
    

    thanks Jan!