I am using next.js. I have json file with a string:
{
"title":"Just a text with <b>accent</b> inside",
}
I want get this string and return a string with <b>
tag inside.
There is my function:
function parseTitle(title) {
let match = title.match(/<b>(.*?)</b>/);
let prev = match[0];
let next = match[1];
let result = title.replace(new RegExp(prev, 'g'), '<b>' + next + '</b>');
return (
result
)
}
And, when I try to use it:
<h1>{parseTitle(votingInfo.title)}</h1>
Of course, I get a string without tags:
Just a text with <b>accent</b> inside
How to fix it?
2
Answers
You can use
dangerouslySetInnerHTML
Edit: I thought I’d understood your question, but it seems you weren’t asking this. In any case, your
parseTitle
function doesn’t do anything — it just returns the string argument unchanged.Here’s an annotated version:
Original answer
The logic of
parseTitle
is wrong. You can implement this using regex lookbehind and lookahead:(?<=<b>)
matches an opening<b>
tag before the match;[^<]+
matches anything that isn’t an opening angle bracket;(?=</b>)
matches a closing</b>
tag after the match. Grabbing[0]
from aRegExpMatchArray
returns the whole matched substring.The function will return an empty string in case it fails to match anything in the
title
passed to it.