-5

Used search but really didn't get it how to split this long regexp on several shorter:

var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
fxvlad
  • 129
  • 2
  • 3
  • 9
  • What would your "several shorter" regexes do? – Matt Burland Mar 16 '18 at 19:02
  • Use the `RegExp` constructor and build your string e.g. from an array with `.join("|")` or any way you want really. – ASDFGerte Mar 16 '18 at 19:09
  • May I ask when you need to split the RegExp into several shorter RegExp if this one works? It seems illogical to me, but you may have your reasons... – Karl-André Gagnon Mar 16 '18 at 19:11
  • Normally people ask how to combine them into one.... What do you want to do to break it up? what is the purpose? What would the smaller ones do? It is unclear what you want, especially since this is a basic reg exp with tons of ors. – epascarello Mar 16 '18 at 19:17
  • It's easy to explain. This code is working absolutely fine but I try to keep my code structured and that line is too long and do't fit on the screen. Actually, I don't understand that code, I took it from another solution from the internet and it works perfectly for me. If I understand that I'd of course use the constructor or smt else. I just want something like that: var x = qwerty'\n'dfdfuiopl; But concatenation does't work with that. – fxvlad Mar 16 '18 at 19:21
  • So the question is how can you add line breaks so it is not one long string.... Not how can you break it up into multiple regular expressions. – epascarello Mar 16 '18 at 19:25

1 Answers1

0

Your regexp is perfectly fine. But in case you want to break it into several short if's you can just do the following (not using regexp at all):

var s = "I'm on IPOD"

mobiles = ["Android", "webOS", "iPod"]
var isMobile = false

for (var i = 0; i < mobiles.length; i++) {
    if (s.toLowerCase().indexOf(mobiles[i].toLowerCase()) >= 0) {
        isMobile = true;
    break;
    }
}

window.alert(isMobile)
zenpoy
  • 19,490
  • 9
  • 60
  • 87
  • could you pls look this https://jsfiddle.net/cktp5Lus/ – fxvlad Mar 16 '18 at 19:39
  • @fxvlad - I looked at it. There's nothing wrong with your original regexp, it does exactly what it should. why changing it? – zenpoy Mar 16 '18 at 19:47
  • Thank you for reply! YEs, it works absolutely fine but I try to keep my code structured and this line is to long and doesn't fit in the screen, that's I want to have smt like concatenating but this dpoesn't work with regex strings. – fxvlad Mar 16 '18 at 19:55
  • Look at the answer here: https://stackoverflow.com/questions/12317049/how-to-split-a-long-regular-expression-into-multiple-lines-in-javascript – zenpoy Mar 16 '18 at 19:59