Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
User Agent Sniffing with Regex in Express has 50% failure rate?

I have a regex defined in my express router to figure out if the UA is mobile or not, and depending on if it is or not, render a different page.

Upon testing on our production site, it has a 25% - 50% failure rate.

I'd like to know the root cause and a fix.

Thank you.

Index.js

  const moment = require('moment');
        var _ = require('lodash');
        var mobileRegex = new RegExp(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i, 'ig');

    ......
    .....
    .....

    router.get('/', function(req, res, next) {

      if (mobileRegex.test(req.headers['user-agent']) == true) {
          console.log('User-Agent: ' + req.headers['user-agent']);
        res.render('index-mobile')
      } else {
        res.render('index');
      }
    });

    router.get('/([\*])', function(req,res, next) { //Escaping * because cloudflare page rules aren't built right
      res.redirect('/')
    });

etc...
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I assume by "failure" you mean mobile devices aren't getting detected?

Browser sniffing just isn't particularly reliable. You could try using something like WhichBrowser but you'd have to keep it up to date. Mozilla themselves recommend simply testing the user agent string for "Mobi", so you might see a smaller failure rate just doing that.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!