2

Hi im trying to get the word right before the coma in a sentence.

For example if the string is:

"Cloudy, and 51 ° F "

I want the output CLOUDY.

How is this done in regex and javascript? I know you have to use the .match command in javascript. Thanks, any help is appricated.

0xSingularity
  • 577
  • 6
  • 36
  • 5
    In the first case, Cloudy is not the second word ? – Jean-François Savard Sep 11 '14 at 17:41
  • String output = checkString.Contains("Mostly") ? checkString.Replace("Mostly","") : checkString; Then just split into array and grab the first element in array. – Andrew Sep 11 '14 at 17:45
  • "(depending on the case)" Depending how? – Yury Tarabanko Sep 11 '14 at 17:45
  • @Jean-FrançoisSavard Thats what I meant by depending on the case. The input is varied. Sometimes cloudy is first and sometime its first. Sometimes the word might not even be cloudy. I want to check if the string contains the word mostly, and if it does get the second word. Else get the first word. If thats not too confusing. ;) – 0xSingularity Sep 11 '14 at 17:50
  • I'm still confused. So, you're looking for the word *Cloudy* and if it doesn't appear, always return the second word? – Mike Christensen Sep 11 '14 at 17:53
  • `str.split(',')[0].split(' ').pop()` in case you need a word before the first comma :) – Yury Tarabanko Sep 11 '14 at 17:54
  • What shall be the output of **Sometimes a bit Cloudy, but mostly sunshine**? – Martin Ernst Sep 11 '14 at 17:56
  • No if _mostly_ appears return the second word (cloudy in this case). Else return the first word (So if mostly is not in the sentence. Cloudy is the first word). I could use Replace("Mostly","") to get rid of _Mostly_ then just match to the first word. – 0xSingularity Sep 11 '14 at 17:57
  • +YuryTarabanko That's what I needed! I just noticed that the word I need is the one right before the comma in both case! How can I get this Word?? – 0xSingularity Sep 11 '14 at 18:00
  • 1
    `var aWord = str.split(',')[0].split(' ').pop()` like this. – Yury Tarabanko Sep 11 '14 at 18:00
  • Removing my close vote in case @YuryTarabanko wants to post an answer. – Mike Christensen Sep 11 '14 at 18:01
  • Yup @YuryTarabanko solved it. The split always returns the word right before the comma. Thanks everyone and sorry for the confusion. If anyone wants this post deleted let me know. – 0xSingularity Sep 11 '14 at 18:04
  • @SunnyD - Instead, I would edit the post to make it more clear. Take what you've explained in the comments and add it to the post. – Mike Christensen Sep 11 '14 at 18:08
  • @MikeChristensen Done! Would you say that is clear enough? – 0xSingularity Sep 11 '14 at 18:17
  • @SunnyD - I'd add another example or two. Such as "Mostly Cloudy," and "Mostly Sunny," and "Rainy," along with which word it should return. But overall I think it's clear. – Mike Christensen Sep 11 '14 at 18:21

1 Answers1

5

I wanted to add an alternative to the split solution in the comments.

Here's the simplest regex you can use:

\S+(?=,)

Demo. In regex syntax, it literally means sequence of non-whitespace characters followed by a comma.

Here's how to use this from JS:

var str = "Mostly cloudy, and 51 ° F";
var matches = str.match(/\S+(?=,)/g); // matches = ["cloudy"]

matches will contain an array with all the words followed by a comma in str. If you only need the first match, remove the g.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158