在 javascript 中生成 SEO 友好的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14107522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:43:54  来源:igfitidea点击:

Producing SEO friendly URL in javascript

javascriptregex

提问by el_pup_le

I have a PHP function that converts a URL to an SEO friendly URL:

我有一个 PHP 函数可以将 URL 转换为 SEO 友好的 URL:

function seo_url($input){
     $input = str_replace(array("'", "-"), "", $input); //remove single quote and dash
     $input = mb_convert_case($input, MB_CASE_LOWER, "UTF-8"); //convert to lowercase
     $input = preg_replace("#[^a-zA-Z0-9]+#", "-", $input); //replace everything non an with dashes
     $input = preg_replace("#(-){2,}#", "", $input); //replace multiple dashes with one
     $input = trim($input, "-"); //trim dashes from beginning and end of string if any
     return $input;
}

I know it's pointless for SEO to do this to a URL in javascript, but for the sake of consistency I want URL's to appear the same in my application. Does anyone have the function handy in javascript? :]

我知道 SEO 对 javascript 中的 URL 执行此操作毫无意义,但为了保持一致性,我希望 URL 在我的应用程序中显示相同。有没有人在javascript中方便的功能?:]

回答by pid

take a look at this javascript module (I'm the author), works in browser and server/nodejs http://pid.github.io/speakingurl/

看看这个 javascript 模块(我是作者),在浏览器和服务器/nodejs 中工作 http://pid.github.io/speakingurl/

hope this helps

希望这可以帮助

回答by Dan Barto

Getting pieces of different solutions together, please consider this alternative code, a one-liner:

将不同的解决方案放在一起,请考虑这个替代代码,一个单行:

function toSeoUrl(url) {
    return url.toString()               // Convert to string
        .normalize('NFD')               // Change diacritics
        .replace(/[\u0300-\u036f]/g,'') // Remove illegal characters
        .replace(/\s+/g,'-')            // Change whitespace to dashes
        .toLowerCase()                  // Change to lowercase
        .replace(/&/g,'-and-')          // Replace ampersand
        .replace(/[^a-z0-9\-]/g,'')     // Remove anything that is not a letter, number or dash
        .replace(/-+/g,'-')             // Remove duplicate dashes
        .replace(/^-*/,'')              // Remove starting dashes
        .replace(/-*$/,'');             // Remove trailing dashes
}

回答by boateng

var url ="Evanston, IN 47531, USA pizza food & wine & music";

document.write(ToSeoUrl(url));
  
function ToSeoUrl(url) {
        
  // make the url lowercase         
  var encodedUrl = url.toString().toLowerCase(); 

  // replace & with and           
  encodedUrl = encodedUrl.split(/\&+/).join("-and-")

  // remove invalid characters 
  encodedUrl = encodedUrl.split(/[^a-z0-9]/).join("-");       

  // remove duplicates 
  encodedUrl = encodedUrl.split(/-+/).join("-");

  // trim leading & trailing characters 
  encodedUrl = encodedUrl.trim('-'); 

  return encodedUrl; 
}