19

I recently stumbled on this issue that, though ran well on iOS, could not run on Android, even though I use the same code base and run them in parallel.

Error log on react native 0.64.4

Note that (number + '') always returns a string, and this code runs on iOS.

Trying the regex with the global flag "g":

string.replace(/searchString/g, replaceString)

like some suggested StackOverFlow answers does not work either.

Can you guys guess the reasons and provide solutions?

4
  • 2
    Can you check if android has hermes enabled and iOS has not? might be related to javascript engine differences on calling Array.prototype methods Commented Sep 23, 2021 at 16:18
  • 2
    What a strange bug!? I'm running into this issue as well. Commented Dec 17, 2021 at 20:21
  • Same here. I really would like to know why. Commented Jun 1, 2022 at 21:39
  • Still an issue on "react-native": "0.65.1". Had to use .replace( instead but would like to understand why is this happening only on Android Commented Aug 8, 2022 at 10:29

3 Answers 3

24

Alternative way to do this with the split and join functions.

string.split("searchString").join("replaceString");
Sign up to request clarification or add additional context in comments.

1 Comment

Just want to add that this method would also work with RegEx. Excellent!
4

replace method works perfectly in the latest React Native version. Please note you need to use string literals (`) instead of quotes (") for string variables.

string.replace(`/${searchString}/g`, replaceString);

Tested on the following React Native version:

react-native-cli: 2.0.1
react-native: 0.64.3

Comments

1

TL;DR

  1. Check android/app/build.gradle
    • Search your project for: enableHermes
  2. Change to: true
  3. Clean and Rebuild project

My default:

project.ext.react = [
    enableHermes: false,  // clean and rebuild if changing
]

Changed to:

project.ext.react = [
    enableHermes: true,  // clean and rebuild if changing
]

I am also facing this issue. It works fine on iOS but with Android, it throws an error.

As suggested by the first comment to OP question, @Krismu, I check if Hermes is enabled. Hermes is a Javascript engine that is bundled with React-native to ensure compatibility and efficiency.

React-native documentation says that is enabled by default and you can opt out.

https://reactnative.dev/docs/hermes#switching-back-to-javascriptcore

However, my android/app/build.gradle file was set to false by default, making my android app not use Hermes.

This solved the problem for me. No need to polyfill, use just 'replace()' or change any code for that matter...

Verison info:

  • "react": "18.0.0",
  • "react-native": "0.69.7",

2 Comments

I find it odd that this would exist within Hermes, and not JS Core. This function is native to JS, and should be so regardless of the platform.
JS is standardized in the standard called ECMAScript. replaceAll was only very recently introduced into the standard (in the 2021 edition), so not all JS engines support it yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.