Why is string.replaceAll() not a function on Android React Native?
Asked Answered
F

3

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?

Fortuity answered 23/9, 2021 at 9:1 Comment(4)
Can you check if android has hermes enabled and iOS has not? might be related to javascript engine differences on calling Array.prototype methodsDelicatessen
What a strange bug!? I'm running into this issue as well.Extern
Same here. I really would like to know why.Liselisetta
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 AndroidHarakiri
V
23

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

string.split("searchString").join("replaceString");
Vanderhoek answered 23/9, 2021 at 9:42 Comment(1)
Just want to add that this method would also work with RegEx. Excellent!Fortuity
G
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
Gruchot answered 18/5, 2022 at 6:35 Comment(0)
C
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",
Calandracalandria answered 19/11, 2022 at 0:37 Comment(2)
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.Fortuity
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.Slavin

© 2022 - 2024 — McMap. All rights reserved.