Skip to content

Instantly share code, notes, and snippets.

@NKid
Last active December 10, 2015 23:28
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 NKid/0d2a47593a335424ea93 to your computer and use it in GitHub Desktop.
Save NKid/0d2a47593a335424ea93 to your computer and use it in GitHub Desktop.
join, split 應用
function getBaseName(str) {
var segs = str.split('.');
if(segs.length > 1) segs.pop();
return segs.join('.');
}
function getExtension(str) {
var segs = str.split('.');
if(segs.length <= 1) return '';
return segs.pop();
}
console.log(getBaseName('my.example.js')); //my.example
console.log(getExtension('my.example.js')); //js
var str = 'welcome_to_my_world'.split('_').join('-');
console.log(str); //welcome-to-my-world
var str = 'welcome_to_my_world'.replace(/_/g, '-');
console.log(str); //welcome-to-my-world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment