I have a function that looks at a string of content & array of documents and finds any matches, then the user can accept or reject that match (another function). When the user accepts this match, see acceptLink function below, then I would like it to replace the first match and not all matches.
Example Content: "The first time I went home was when I went to see my dog at home."
Example Array: {name: 'Home', link: 'https://www.home.com/'}
Expected output: "The first time I went < a href=https://www.home.com/>Home< / a > was when I went to see my dog at home."
const acceptLink = (a) => {
var reg = new RegExp('(' a.name ')', 'gi');
const aTag = '<a href=' a.link '>' a.name '</a>'
setContent(content.replace(reg, aTag));
const replaceContent = content.replace(reg,aTag)
const filtered = allLinks.filter(obj => {
return obj.name !== a.name;
});
Have I missed something here?
CodePudding user response:
Remove g
const acceptLink = (a) => {
var reg = new RegExp('(' a.name ')', 'i');
const aTag = '<a href=' a.link '>' a.name '</a>'
setContent(content.replace(reg, aTag));
const replaceContent = content.replace(reg,aTag)
const filtered = allLinks.filter(obj => {
return obj.name !== a.name;
});