1

not sure what's going on here.

I have code that works fine in iOS:

    const bolds = /\*\*([^\*]+)\*\*/g;
    const italics = /[^\*]\*([^\*]+)\*/g;
    const images = /\[\[\[(.*)\]\]\]/g;
    const footnotes = /xx(\d*)/g;
    const hyperlinks = /[^\[](https?:\/\/[^\s]+)/g;

    const matchRegexes = {
        hyperlinks: [...text.matchAll(hyperlinks)],
        bolds: [...text.matchAll(bolds)],
        italics: [...text.matchAll(italics)],
        images: [...text.matchAll(images)],
        footnotes: [...text.matchAll(footnotes)],
    };

I'm using it to identify a custom markup / certain text patterns.

When I attempt to load the View that uses this code I get this error pointed at the first text.matchAll

undefined is not a function

What is going on here? It looks like matchAll is relatively new to the spec, but it's working for ios, why would it not be included with android?

Christopher Reid
  • 4,318
  • 3
  • 35
  • 74
  • what is console log text? – wangdev87 Nov 09 '20 at 03:47
  • Are you asking for a solution, or an explanation? (Probably only Android internal devs can answer *why*) – CertainPerformance Nov 09 '20 at 03:47
  • @CertainPerformance ideally both. The workaround is obviously to implement this without matchAll, but I don't understand why I can't. This more of a problem for react-native devs than android devs though as it looks like the underlying javascript engine is the issue. – Christopher Reid Nov 09 '20 at 03:55
  • @WilliamWang what console log text? I'm not logging anything. And these expressions are unloggable because they throw an error before having any value. – Christopher Reid Nov 09 '20 at 03:56
  • have you ever found a solution ? same issue here: https://stackoverflow.com/q/64935512 – ssc Nov 20 '20 at 19:22
  • @ssc this happens because of an older version of javascript is being run on the android that doesn't include `matchAll`. Older versions of node have the same problem. Im not sure if its possible to update the js backend on android, but I'm using this in the meantime: https://www.npmjs.com/package/string.prototype.matchall – Christopher Reid Nov 22 '20 at 13:20

2 Answers2

1

It looks like matchAll is not supported by the RN's JS engine on Android, but it will likely be available in RN 0.64 [0].

Until then, you can gather all matches yourself manually using RegExp.exec() [1]:

const str = 'the dog doggedly jumped over the moon';
const regex = new RegExp('dog[a-z]*', 'g');
let match;
let matches = [];
while ((match = regex.exec(str)) !== null) {
  matches.push(match[0]);
}

console.log(matches); // [ "dog", "doggedly" ]

This works on both iOS and Android.

pdsouza
  • 621
  • 6
  • 9
1

Also you can use an external library:

https://www.npmjs.com/package/string.prototype.matchall

import matchAll from 'string.prototype.matchall';
const array = [... matchAll(str, regexp)];

It worked for me.

Erick Gutierrez
  • 446
  • 3
  • 7