-2

If I had the string

"Nice to meet you"

Would there be a way to check if the word before "you" was "meet", and return a Boolean value? (e.g. if word before you == meet)

  • 9
    Yes, it would be possible. – PM 77-1 Nov 07 '18 at 23:23
  • Use [String.prototype.includes](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript/14193950#14193950) – SuperStormer Nov 07 '18 at 23:27
  • You might try looking for "you" with [indexof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) and then using that index with [lastIndexOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf) to look for "meet" – David Yates Nov 07 '18 at 23:30
  • 1
    Welcome to Stack Overflow. Please read our [How to Ask a Question](https://stackoverflow.com/help/how-to-ask) post so that you can understand what we expect of questions. We look for you to do your research first and then post a ***specific*** question about the problem you are having. We also expect that you'll have done enough research that you'll have made an attempt at a solution and we like to see those attempts posted along with your question. – Scott Marcus Nov 07 '18 at 23:35

3 Answers3

1

ES6 or above only solution:

Use String.prototype.includes. Regex is overkill for this type of thing.

Use string.includes("meet you")

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • 1
    Please don't answer questions that don't make any attempt at a solution or just ask general questions. These questions are ripe to be closed and answering them just encourages more poor questions. – Scott Marcus Nov 07 '18 at 23:32
  • Please keep in mind that this won't work on Internet Explorer and some older browsers. Try indexOf() – Petar Vasilev Nov 07 '18 at 23:32
0

Are you looking for Regular expressions, OP? With regular expressions you can do much more than just checking if a particular word appears before another in a string. You can check if there is another word in between them for example.

Would something like this satisfy your needs? The regex returns true if the word you appears after meet with anything/nothing in between them.

let hay = "Nice to meet you";
let needle = /meet.*you/;
if( needle.test(hay) ) {
    // yes it does
}

You can find explanations of RegEx in JS on MDN page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Why use regex when indexOf is much faster? – Petar Vasilev Nov 07 '18 at 23:31
  • indexOf would require that you also check the index of the left is right - left.length+1. OP's question requires word A to come immediately before B, not just earlier in the general sense. – Bricky Nov 07 '18 at 23:33
  • Yes, but I extrapolated from OP's answer. What if the string is "Nice to meet all of you."? What if the space is NBSP or newline or tab? Regex is much more robust, although slower solution. In general RegEx is overkill but it might be helpful to OP nonetheless, e.g. if they want to exclude the cases I mentioned. – Dharman Nov 07 '18 at 23:37
0

Yes, it would be.

 var string = "Nice to meet you",
     split = string.split(" "),
     answer = split[split.indexOf("you") - 1] == "meet";

console.log('"Meet" is before "you"?', answer);
Petar Vasilev
  • 282
  • 1
  • 11
  • 1
    Please don't answer questions that don't make any attempt at a solution or just ask general questions. These questions are ripe to be closed and answering them just encourages more poor questions. – Scott Marcus Nov 07 '18 at 23:32
  • This doesn't answer op's question, as this answers it the word 'meet' is earlier in the string than 'you'. It does not tell us if the word before 'you' is the word 'meet'. – Bricky Nov 07 '18 at 23:35
  • Fixed my answer. – Petar Vasilev Nov 07 '18 at 23:38
  • 1
    And now you see why regex is the preferable approach for string search. Your answer is not readable or transparent at what it is attempting to accomplish. It is also very brittle and not maintainable. What if we want to handle more than one space, or new line characters in the string - both of which are common things we would encounter with user-input or natural text. – Bricky Nov 07 '18 at 23:39