How does Stack Overflow generate its SEO-friendly URLs?

后端 未结 21 1856
-上瘾入骨i
-上瘾入骨i 2020-11-22 04:27

What is a good complete regular expression or some other process that would take the title:

How do you change a title to be part of the URL like Stack

21条回答
  •  广开言路
    2020-11-22 04:48

    Brian's code, in Ruby:

    title.downcase.strip.gsub(/\ /, '-').gsub(/[^\w\-]/, '')
    

    downcase turns the string to lowercase, strip removes leading and trailing whitespace, the first gsub call globally substitutes spaces with dashes, and the second removes everything that isn't a letter or a dash.

提交回复
热议问题