roger
roger

Reputation: 9893

How to make one line coffee script code to multiline?

I tried to use coffee script recently, but I have some code like this

if userAgent.match(/iPad/i) or userAgent.match(/iPhone/i) or userAgent.match(/iPoid/i) or userAgent.match(/Android/i) or userAgent.match(/IEMobile/i) or userAgent.match(/BlackBerry/i)
    console.log("This is a mobile device")
else
    console.log("This is not mobile device")

So the first line is very long, I want to make it multi-line.Maybe some code like this is much better, but as you know, this is wrong in coffee script.

# This is the wrong code
if userAgent.match(/iPad/i) 
    or userAgent.match(/iPhone/i) 
    or userAgent.match(/iPoid/i) 
    or userAgent.match(/Android/i) 
    or userAgent.match(/IEMobile/i) 
    or userAgent.match(/BlackBerry/i)
    console.log("This is a mobile device")
else
    console.log("This is not mobile device")

or some code like this:

# This is also wrong
if userAgent.match(/iPad/i) or 
  userAgent.match(/iPhone/i) or 
  userAgent.match(/iPoid/i) or 
  userAgent.match(/Android/i) or 
  userAgent.match(/IEMobile/i) or 
  userAgent.match(/BlackBerry/i)
    console.log("This is a mobile device")
else
    console.log("This is not mobile device")

So any way to correct this?

Upvotes: 1

Views: 105

Answers (1)

manojlds
manojlds

Reputation: 301147

Also, you are much better off if you write the regex like this:

if userAgent.match /iPad|iPhone|iPod|Android|IEMobile|BlackBerry/i

Upvotes: 2

Related Questions