Skip to main content
3 of 5
added 42 characters in body
Taylor Raine
  • 8.8k
  • 2
  • 28
  • 51

JavaScript (ES6), 76 Bytes

s=s.replace(/(\S+ +(\S+))\(s\)/g,(_,a)=>a+("s"[+/^(1|one|an?)\b/i.test(a)]||""))

74 Byte version (less robust, \b)

s=s.replace(/(\S+ +(\S+))\(s\)/g,(_,a)=>a+("s"[+/^(1|one|an?)/i.test(a)]||""))

This is a modified version of ETHproductions' (I don't have 50 rep.)

Explanation

  • /(\S+ +(\S+))\(s\)/g - the actual pattern to look for (amount object(s))
  • (_,a)=>a - _ is a catch all variable, a is the (\S+ +(\S+))
  • "s"[+/^(1|one|an?)/i.test(a)]||"" - instead of slicing the array, just make a dummy array and get the index (+/.../.test returns a number)
    • should "s"[+/^(1|one|an?)/i.test(a)] return undefined (true, or 1 for the test) return ""