splitメソッドを使用すると、指定されたセパレータ(この場合は "")で文字列を分割し、返された配列の最後の部分文字列を取得できます。
これは、あなたが文字列の他の部分を使用したいと、それも読みやすい場合は良い方法です:
// setup your string
var string1 = "hello any body from me";
// split your string into an array of substrings with the " " separator
var splitString = string1.split(" ");
// get the last substring from the array
var lastSubstr = splitString[splitString.length - 1];
// this will log "me"
console.log(lastSubstr);
// ...
// oh i now actually also need the first part of the string
// i still have my splitString variable so i can use this again!
// this will log "hello"
console.log(splitString[0]);
これはなくても良い方法です部分文字列の残りの部分については、素早く汚れて書くことを好む場合:
// setup your string
var string1 = "hello any body from me";
// split your string into an array of substrings with the " " separator, reverse it, and then select the first substring
var lastSubstr = string1.split(" ").reverse()[0];
// this will log "me"
console.log(lastSubstr);
[JavaScript文字列の最後のアンダースコアの前にテキストのみを返します](http://stackoverflow.com/questions/38208101/return-only-text-before-last-underscore-in-javascript-string) –