My intention is to build a parsing function which searches within a string (think blog post) for any substrings wrapped around unique identifiers in parentheses, like (i).
My current implementation isnt working. Would truly appreciate any help!
let text = "Hello there (i)Sir(i)";
let italics = /\(i\)(.?*)\(i\)/gi;
let italicsText = text.match(italics);
// text.replace(italics, <i>)
>Solution :
You can use replace here with regex as:
\((.*?)\)/g
Second argument to replace is a replacer function.
replace(regexp, replacerFunction)
A function to be invoked to create the new substring to be used to
replace the matches to the given regexp or substr.
How arguments are passed to replacer function
let text = 'Hello there (i)Sir(i)';
let italics = text.replace(/\((.*?)\)/g, (_, match) => `<${match}>`);
console.log(italics);
let text2 = 'Hello there (test)Sir(test)';
let italics2 = text2.replace(/\((.*?)\)/g, (_, match) => `<${match}>`);
console.log(italics2);