4

I am doing some logic for the last word that is on the sentence. Words are separated by either space or with a '-' character.

What is easiest way to get it?

Edit

I could do it by traversing backwards from the end of the sentence, but I would like to find better way

0

6 Answers 6

17

Try splitting on a regex that matches spaces or hyphens and taking the last element:

var lastWord = function(o) {
  return (""+o).replace(/[\s-]+$/,'').split(/[\s-]/).pop();
};
lastWord('This is a test.'); // => 'test.'
lastWord('Here is something to-do.'); // => 'do.'

As @alex points out, it's worth trimming any trailing whitespace or hyphens. Ensuring the argument is a string is a good idea too.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for regex in split() and pop(). Though you should probably trim the string first, this should do it... /^\s+|\s+$/g.
@alex: good idea but I suppose leading whitespace doesn't really matter.
Nice one, I knew that there would be nice way to do it. I really didn't want to do looping +1
6

Using a regex:

/.*[\s-](\S+)/.exec(str)[1];

that also ignores white-space at the end

2 Comments

Can you explain why index 1 has the result?
because it's the result of a regex. index[0] is the entire string that was matched, [1] is the first set of parens (what is in (\S+))
0

Have you tried the lastIndexOf function http://www.w3schools.com/jsref/jsref_lastIndexOf.asp

Or Split function http://www.w3schools.com/jsref/jsref_split.asp

2 Comments

The thing is that those except only one of the characters required. More powerful string classes normally have split that takes array of characters..
well you have to do it twice, then see which one the last one. Call lastIndexOf(' ') then lastIndexOf('-') Then use which ever resust is the biggest
0

Here is a similar discussion have a look

Comments

0

You can try something like this...

<script type="text/javascript">
var txt = "This is the sample sentence";
spl = txt.split(" ");
for(i = 0; i < spl.length; i++){
    document.write("<br /> Element " + i + " = " + spl[i]); 
}
</script>

1 Comment

you have to replace txt.split(" ") by txt.split("-")
0

Well, using Split Function

  string lastWord = input.Split(' ').Last();

or

string[] parts = input.Split(' ');
string lastWord = parts[parts.Length - 1];

While this would work for this string, it might not work for a slightly different string, so either you'll have to figure out how to change the code accordingly, or post all the rules.

string input = ".... ,API";

here, the comma would be part of the "word".

Also, if the first method of obtaining the word is correct, ie. everything after the last space, and your string adheres to the following rules:

Will always contain at least one space
Does not end with one or more space (in case of this you can trim it)

then you can use this code that will allocate fewer objects on the heap for GC to worry about later:

string lastWord = input.Substring(input.LastIndexOf(' ') + 1);

I hope its help

2 Comments

better u can use the regex answer from maerics
var str = "hi anand" alert(str.replace(/[\s-]+$/,'').split(/[\s-]/).pop());

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.