Skip to main content
added 2 characters in body
Source Link
Taylor Raine
  • 8.8k
  • 2
  • 28
  • 51

JavaScript (SpiderMonkey), 82 bytes

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

Try it online!

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)] return undefined (true, or 1 for the test) return ""

JavaScript (SpiderMonkey), 82 bytes

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

Try it online!

78 Byte version (less robust)

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 ""

JavaScript (SpiderMonkey), 82 bytes

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

Try it online!

78 Byte version (less robust)

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 ""
edited "try it online"
Source Link

JavaScript (ES6), 76 Bytes

JavaScript (SpiderMonkey), 82 bytes

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

Try it online!

7478 Byte version (less robust, \b)

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)] return undefined (true, or 1 for the test) return ""

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 ""

JavaScript (SpiderMonkey), 82 bytes

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

Try it online!

78 Byte version (less robust)

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 ""
added 42 characters in body
Source Link
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)]||""))

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)]||""))

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 ""

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 ""

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 ""
corrected explanation
Source Link
Loading
Source Link
Loading