Skip to content

Instantly share code, notes, and snippets.

@JerryC8080
Forked from shanelau/index.javascript
Last active April 21, 2016 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JerryC8080/45563cefeccbdc2493c7 to your computer and use it in GitHub Desktop.
Save JerryC8080/45563cefeccbdc2493c7 to your computer and use it in GitHub Desktop.
中文转拼音
var unidecode = require('unidecode');
var safeString = function (string) {
string = string.trim();
// Remove non ascii characters
string = unidecode(string).trim();
// Remove URL reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\%<>|^~£"`
string = string.replace(/[:\/\?#\[\]@!$&'()*+,;=\\%<>\|\^~£"]/g, '')
// Replace dots and spaces with a dash
.replace(/(\s|\.)/g, '-')
// Convert 2 or more dashes into a single dash
.replace(/-+/g, '-')
// Make the whole thing lowercase
.toLowerCase();
console.log(string);
return string;
}
safeString('hello world');
safeString(' 我love天南门');
//hello-world
//wo-lovetian-nan-men
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment