-4

I'd like to make an if statement like this:

if(playerMessage == specificallyOnlyTwoWords) {
  // do something
}

This means, when for example, "playerMessage" has a value of "Fruit Ninja" it'll will do the code in the if statement, but if the value of the variable is "Great Fruit Ninja" the code in the if statement wont be run.

Two words is a word divided by a blank space or

If possible, if it could pay attention punctuation would be greatly appreciated. "Punctuation" is any character that is not a letter. So for example: A is a letter, ; is punctuation

I'm using JavaScript.

Tigerrrrr
  • 526
  • 6
  • 24
  • 4
    please add an example of the string and the two words. – Nina Scholz Jun 27 '19 at 19:37
  • 5
    Please post a working example after reading: [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Randy Casburn Jun 27 '19 at 19:38
  • 2
    your question is too vague. What qualifies as two words with punctuation? one.two, one. two, one two., .one two, one@#(&$%^@#!(@*&two, etc. – Randy Casburn Jun 27 '19 at 19:39
  • 2
    `Fruit-Ninja` one word or two? `Fruit:Ninja`? It's not clear what you mean by ignore punctuation. Do you want to ignore it in the sense that it doesn't break words or that it does break word. – Mark Jun 27 '19 at 19:46
  • @NinaScholz | I've edited the question to show an example. – Tigerrrrr Jun 27 '19 at 19:47
  • 1
    Split on `" "`, get length? `playerMessage.split(" ").length === 2` – Tyler Roper Jun 27 '19 at 19:48
  • @MarkMeyer | Two words is a word divided by a blank space or " " – Tigerrrrr Jun 27 '19 at 19:48
  • 3
    Could you go more in-depth on what you mean by "**If possible, if it could ignore punctuation would be greatly appreciated.**"? – Get Off My Lawn Jun 27 '19 at 19:49
  • @GetOffMyLawn | I've edited the question to go more in-depth. – Tigerrrrr Jun 27 '19 at 19:53
  • 1
    Yes... I know the difference between punctuation and non-punctuation :) What do you mean be ignoring them though? What are some examples? – Get Off My Lawn Jun 27 '19 at 19:55
  • @GetOffMyLawn | If the player sends "Hello, Johnny" then the code won't be ran but if the player sends "Hello Johnny" the code will be ran. – Tigerrrrr Jun 27 '19 at 19:59
  • 1
    @Tigerrrrr that doesn't sound like **ignoring** punctuation. That sounds like paying attention to punctuation. You have different results and the only difference in input is the punctuation. – Mark Jun 27 '19 at 20:00
  • 1
    @MarkMeyer Not to mention, OP accepted an answer that clearly contradicts his latest instruction. – Tyler Roper Jun 27 '19 at 20:06
  • @TylerRoper | I tested it and it doesn't. – Tigerrrrr Jun 27 '19 at 20:08
  • [I disagree](https://jsfiddle.net/wf0Lb2o9/). The string `"Hello, Johnny"` will be considered two words despite your latest comment saying that it shouldn't be. In fact, Rodrigo's *original* answer behaved as you requested above - it wasn't until you then asked if he could change it that it stopped working. – Tyler Roper Jun 27 '19 at 20:10

3 Answers3

2

Here's a simple way. Count the number of global matches of white space and return true if there is only one.

var strings = ['two words','one more word', 'a bunch of words']
strings.filter(str => {
 console.log(str.match(/\s/g).length === 1)
})
stever
  • 1,232
  • 3
  • 13
  • 21
2

You can split on non-word characters by using \W then you can test the length of the split string:

let strs = ['Hello, Johnny', 'Hello,Johnny', 'Hello Johnny', 'Hello \n World', 'Hello   World']

strs.forEach(str => console.log(str, str.split(/\W/).length === 2))

console.log('-- Trim Witespace ---')

// Replace extra whitespace
strs.forEach(str => console.log(str, str.replace(/\s\s+/g, ' ').split(/\W/).length === 2))
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
1
if (playerMessage.split(/\W+/).length === 2) {
   // do something
}

It will ignore everything that's not a letter.

rodrigocfd
  • 6,450
  • 6
  • 34
  • 68