String.matchAll(regex) causes an error in Expo client on Android. How to fix it?

Hi! In web everything is working. But, in expo client for Android appears an error:

TypeError: (subsText + '\n\n').matchAll is not a function. (In '(subsText + '\n\n').matchAll(cueRegexTemplate)', '(subsText + '\n\n').matchAll' is undefined)

I have read that String.matchAll is ES2020 feature. Is there a way to get it working in my app?

Perhaps this will work:

1 Like

Thank you! Can you give me some clue please, how to implement it to code in npm_module? Should I rewrite it? string.matchAll(regex) to matchAll(string, regex) ?
May be there is a way to set up es2020 ?

Yes. I have not used it before, but I gave it a try now:

$ expo install string.prototype.matchall
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import matchAll from 'string.prototype.matchall';

export default function App() {
  // from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll
  // with str.matchAll(...) converted to matchAll(str, ...)
  const regexp = /t(e)(st(\d?))/g;
  const str = 'test1test2';

  const array = [... matchAll(str, regexp)];

  console.log(array[0]);
  // expected output: Array ["test1", "e", "st1", "1"]

  console.log(array[1]);
  // expected output: Array ["test2", "e", "st2", "2"]
    
  return (
    <View style={styles.container}>
      <Text>Open up App.js to start working on your app!</Text>
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Output:

Array [
  "test1",
  "e",
  "st1",
  "1",
]
Array [
  "test2",
  "e",
  "st2",
  "2",
]
1 Like