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?

6
  • what is console log text? Commented Nov 9, 2020 at 3:47
  • Are you asking for a solution, or an explanation? (Probably only Android internal devs can answer why) Commented Nov 9, 2020 at 3: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. Commented Nov 9, 2020 at 3: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. Commented Nov 9, 2020 at 3:56
  • have you ever found a solution ? same issue here: stackoverflow.com/q/64935512 Commented Nov 20, 2020 at 19:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.