簡體   English   中英

如何用空格替換破折號,並用破折號替換雙破折號?

[英]How to replace dashes with spaces, and double dashes with dashes?

我正在用流星寫一個CRUD,我想要漂亮的URL,例如

www.mysite.com/John-Doe。

(實際上,我真正的偏好是www.mysite.com/JohnDoe,但是實現起來可能會有些困難。)

為此,我需要能夠將“ John-Doe”翻譯成“ John Doe”,以及從“ John-Doe-Smith”翻譯成“ John Doe-Smith”。

我當然可以使用“-”的初步替換來替換一些臨時字符,但尋找一種更優雅的解決方案。

(編輯:寫完這篇文章后,我才意識到我可以清理名稱以將多個空格和破折號先合並為一個;但是現在我對更通用的答案感到好奇)。

您可能需要看一下流星可用的underscore.string庫,它可以在meteor add underscorestring:underscore.string

// Replace dashes with spaces:
s.humanize("no-dash");
// => "no dash"

// Dashes to camel case:
s.camelize("John-Doe");
// => "JohnDoe"

// Double dashes to single dashes:
s.replaceAll("John-Doe--Smith", "--", "-");
// => "John-Doe-Smith"

// Join two names with dashes:
s.join("-", "John", "Doe");
// => "John-Doe"

在許多情況下,有多種方法可以達到相同的結果,包括比上述方法更為優雅或量身定制的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM