views:

144

answers:

4

Hy, is there a way to find the first letter of the last word in a string? The strings are results in a XML parser function. Inside the each() loop i get all the nodes and put every name inside a variable like this: var person = xml.find("name").find().text()

Now person holds a string, it could be:

  • Anamaria Forrest Gump
  • John Lock

As you see, the first string holds 3 words, while the second holds 2 words.

What i need are the first letters from the last words: "G", "L",

How do i accomplish this? TY

A: 

EXAMPLE:

string_1 = Anamaria Forrest Gump string_2 = John Lock string_3 = AAA BBB ZZZ UIOPO QQUI V


I need: "G", "L" , "V"

Those are the first letters of the last words

Joshua
If you need to add more information to your question, please edit your question. StackOverflow is not a forum—you should only answer your own question if you *have* actually answered your own question.
Steve Harrison
Sorry man, didn't saw edit before
Joshua
+1  A: 

This should do it:

var person = xml.find("name").find().text();

var names = person.split(' ');
var firstLetterOfSurname = names[names.length - 1].charAt(0);
Mark B
If needed, you can split using re: `person.split(/\s+/)`
strager
sorry, didn't saw edit -> now i seeTHX mark B apsolutely wonderfull! I needed a few minutes to understand it. Split criteria is space(' '), so we get substrings inside names. You get the last word with (names.length-1) because length counts from 1 i supose, and get the first char!!! BRILLIANT! MONSTER KILL, UNSTOPPABLE!I already begon with javascript looping trough all letters and counting, but this is much better. THX
Joshua
As arrays are zero-based, `names.length` gives us an index outside the bounds of the array, hence `names.length - 1`. Other than that you're spot on.
Mark B
yes i understood it well thx i'm using this tehnique now
Joshua
A: 

You can hack it with regex:

'Marry Jo Poppins'.replace(/^.*\s+(\w)\w+$/, "$1");      // P
'Anamaria Forrest Gump'.replace(/^.*\s+(\w)\w+$/, "$1"); // G

Otherwise Mark B's answer is fine, too :)


edit:

Alsciende's regex+javascript combo myString.match(/(\w)\w*$/)[1] is probably a little more versatile than mine.

regular expression explanation

/^.*\s+(\w)\w+$/
^     beginning of input string
.*    followed by any character (.) 0 or more times (*)
\s+   followed by any whitespace (\s) 1 or more times (+)
(     group and capture to $1
  \w  followed by any word character (\w)
)     end capture
\w+   followed by any word character (\w) 1 or more times (+)
$     end of string (before newline (\n))

Alsciende's regex

/(\w)\w*$/
(     group and capture to $1
  \w  any word character
)     end capture
\w*   any word character (\w) 0 or more times (*)

summary

Regular expressions are awesomely powerful, or as you might say, "Godlike!" Regular-Expressions.info is a great starting point if you'd like to learn more.

Hope this helps :)

macek
DOMINATE! but it's a little hard to understand now these are regular expressions i suppose? I started to learn reg.expressions once but that was such a mess...i thought: Leave the snake alone..if you don't need it. But it's very cool, maybe you could explain or link to a tutor?thx
Joshua
A: 

This solution will work even if your string contains a single word. It returns the desired character:

myString.match(/(\w)\w*$/)[1];

Explanation: "Match a word character (and memorize it) (\w), then match any number of word characters \w*, then match the end of the string $". In other words : "Match a sequence of word characters at the end of the string (and memorize the first of these word characters)". match returns an array with the whole match in [0] and then the memorized strings in [1], [2], etc. Here we want [1].

Regexps are enclosed in / in javascript : http://www.w3schools.com/js/js_obj_regexp.asp

Alsciende
DOUBLEKILL! - UNHUMAN!! - GODLIKE..what means: / and \what means: W and Swhat means: $ and * and +Is it possible that you translate myString.match(/(\w)\w*$/)[1];in words (like a pseudo solution so that i can understand it)
Joshua