Bold and Italics in Dynamic text

I use the inkle format for my game which uses ** as bold and _ as italics. I wanted to render these correctly in Rive.

To do this, you can use a dynamic font and change the slant and weight properties:

Create a start and end property in the view model for each, then bind these in runtime for selected words. Here’s my code to find the sections:

const parseFormatting = (input) => {
                    let clean = "";
                    let last = 0;
                    let removedSoFar = 0;
                    let italStart = 0, italEnd = 0, boldStart = 0, boldEnd = 0;
                    let italSet = false, boldSet = false;

                    const re = /(\*\*[^*]+\*\*|_[^_]+_)/g;
                    let m;
                    while ((m = re.exec(input)) !== null) {
                        // append text before match
                        clean += input.slice(last, m.index);
                        const token = m[0];
                        let inner, startInFinal;

                        if (token.startsWith("**")) {
                            inner = token.slice(2, -2);
                            startInFinal = (m.index - removedSoFar);
                            if (!boldSet) {
                                boldStart = startInFinal;
                                boldEnd = boldStart + inner.length;
                                boldSet = true;
                            }
                            clean += inner;
                            removedSoFar += token.length - inner.length; // 4
                        } else if (token.startsWith("_")) {
                            inner = token.slice(1, -1);
                            startInFinal = (m.index - removedSoFar);
                            if (!italSet) {
                                italStart = startInFinal;
                                italEnd = italStart + inner.length;
                                italSet = true;
                            }
                            clean += inner;
                            removedSoFar += token.length - inner.length; // 2
                        }
                        last = re.lastIndex;
                    }
                    clean += input.slice(last);

                    // If not found, keep zeros
                    return {
                        text: clean,
                        italStart: italSet ? italStart : 0,
                        italEnd: italSet ? italEnd : 0,
                        boldStart: boldSet ? boldStart : 0,
                        boldEnd: boldSet ? boldEnd : 0
                    };
                };

So far this only works for one bolded and one italicized section for each run, but I could probably add more text modifiers as needed.