Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native regex causes a crash on iOS 11.2

Tags:

react-native

Description

The app crashes only on iOS 11.2 giving me unhandled JS Exception: Invalid regular expression: unrecognized character after (? while not in debug mode. I've tested the same code on iOS 13 and it works fine. I eventually figured out which regex were causing the problem and I found out that all regex of this type (?<value>\d{2}\/\d{2}\/\d{4}) were causing the crash they are stored as value of a Javascript Object in a separate .js file. I'm asking for a solution that doesn't require changing all the regex.

I don't understand why the behaviour on iOS 13 is different from iOS 11.2.

Also I found a similar issue with lookbehind regex at this link Crash if not in debug on android and iOS.

What I've already tried

  • First thing first I tried to update React Native to the latest version 0.61.5, but it didn't solve my issue
  • I've tried to replicate the same issue in a fresh new project and it didn't crash
  • Tried to remove all the regex, this worked, but the regex are part of a big and important functionality of the app

Environment

System:
    OS: macOS 10.15.2
    CPU: (8) x64 Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
    Memory: 461.18 MB / 16.00 GB
  Binaries:
    Node: 13.3.0 - /usr/local/Cellar/node/13.3.0/bin/node
    Yarn: 1.19.2 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 28, 29
      Build Tools: 28.0.3, 29.0.2
      System Images: android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.5977832
    Xcode: 11.3/11C29 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.8.6 => 16.8.6 
    react-native: ^0.60.5 => 0.60.5 
  npmGlobalPackages:
    react-native-cli: 2.0.1

Screenshots

iPhone 6s simulator running iOS 11.2

Thanks for reading 🤞

like image 516
fannolo Avatar asked Jan 03 '20 20:01

fannolo


People also ask

What is the use of regex in React Native?

Usually such patterns are used by string-searching algorithms for “find” or “find and replace” operations on strings, or for input validation. React Native codebase same to javascript then syntax are same for both, we have to write and regex in veriable and use javascript test functionalities to validate.

What's the difference between React-Native-hyperlink and regex-defined-hyperlinks?

Text component for React Native with regex defined hyperlinks. Heavily inspired from react-native-hyperlink. The difference is that with react-native-hyperlink you use linkify which I couldn't configure to detect arbitrary regex without prefix (e.g. '1:00').

How to fix iPhone crashing on Mac?

Attach your iPhone with Mac machine. Select your device from the device list. You will see two option view – crash log and open console. click on the open console and you will see the various log of your device. Now open your app and see the log you will get the crash error in the console window or you can search it via Undefined word.

How to find a crash report in Xcode?

First of all, we try to find a crash report in Xcode. Navigate to Xcode->Window->Devices and Simulators open it. Attach your iPhone with Mac machine. Select your device from the device list. You will see two option view – crash log and open console. click on the open console and you will see the various log of your device.


2 Answers

What i would suggest is if you see that its not working on some specific ios version , check for the ios version and if its ios 13 and above use that regex or try some other regex which works in 11.2.

You can check the iosVersion by

    import {Platform} from 'react-native';

    const majorVersionIOS = parseInt(Platform.Version, 10);
    if (majorVersionIOS <= 13) {
      console.log('YOu need some other regex');
    } else {
console.log('YOu can use same regex');
}

Hope it helps. feel free for doubts

like image 177
Gaurav Roy Avatar answered Oct 07 '22 11:10

Gaurav Roy


We ended up removing all the regexes from the app, and implemented them in the backend.

like image 1
fannolo Avatar answered Oct 07 '22 12:10

fannolo