javascript - Find the first letter of the last word with jquery inside a string (string can have multiple words) - Stack Overflo

Hy, is there a way to find the first letter of the last word in a string? The strings are results in a

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 acplish this? TY

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 acplish this? TY

Share Improve this question edited Mar 5, 2010 at 10:36 Amber 528k88 gold badges641 silver badges557 bronze badges asked Mar 5, 2010 at 10:26 JoshuaJoshua 311 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

This should do it:

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

var names = person.split(' ');
var firstLetterOfSurname = names[names.length - 1].charAt(0);

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./js/js_obj_regexp.asp

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744358562a4570354.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信