JavaScript (SpiderMonkey), 82 bytes
s=s.replace(/(\S+ +(\S+))\(s\)\B/g,(_,a)=>a+("s"[+/^(1|one|an?)\b/i.test(a)]||""))
78 Byte version (less robust)
s=s.replace(/(\S+ +(\S*))\(s\)/g,(_,a)=>a+("s"[+/^(1|one|an?)/i.test(a)]||""))
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)]
returnundefined
(true
, or1
for the test) return""
- should