3

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

3 Answers 3

5

This should do it:

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

var names = person.split(' ');
var firstLetterOfSurname = names[names.length - 1].charAt(0);
Sign up to request clarification or add additional context in comments.

3 Comments

If needed, you can split using re: person.split(/\s+/)
sorry, didn't saw edit -> now i see THX 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
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.
1

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

1 Comment

DOUBLEKILL! - UNHUMAN!! - GODLIKE.. what means: / and \ what means: W and S what 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)
1

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 :)

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.