var description1 = `Hello world
This is the first test {f19e7a87-3ae9-40d7-b81a-ad5eba8a2975, First Link}
and some words
This is the second test {587d2209-fcf2-448d-b52d-7f0c93e59c8c, Second Link}`;
var HyperValues = ["f19e7a87-3ae9-40d7-b81a-ad5eba8a2975, First Link"
,"587d2209-fcf2-448d-b52d-7f0c93e59c8c, Second Link"];
$.each(HyperValues, function (key, value) {
var hyperLinkId = value.split(",")[0];
var hyperLinkText = value.split(",")[1];
// generate regular expression dynamically
// and test string ends with hyperLinkText + '}'
// this regular is the something like /\{.+?First Link\}/g or /\{.+?Second Link\}/g
// so for HyperValues[0] it will match '{f19e7a87-3ae9-40d7-b81a-ad5eba8a2975, First Link}' only
// for HyperValues[1] it will match '{587d2209-fcf2-448d-b52d-7f0c93e59c8c, Second Link}' only
var Reg = new RegExp('\\{.+?\\' + $.trim(hyperLinkText) + '}','g');
description1 = description1.replace(Reg, '<a id= ' + hyperLinkId + ' ' + 'class="infoBoxPopup" data-toggle="modal" data-target="#moreInfoModal" href="#"> ' + hyperLinkText + '</a>');
});
var description2 = `Hello world
This is the first test {f19e7a87-3ae9-40d7-b81a-ad5eba8a2975, First Link}
and some words
This is the second test {587d2209-fcf2-448d-b52d-7f0c93e59c8c, Second Link}`;
var HyperValues2 = ["f19e7a87-3ae9-40d7-b81a-ad5eba8a2975"
,"587d2209-fcf2-448d-b52d-7f0c93e59c8c"];// remove Fist Link and Second Link
$.each(HyperValues2, function (key, value) {
var hyperLinkId = value.split(",")[0];
var hyperLinkText = value.split(",")[1];//because remove Fist Link and Second Link this will be undefined
// because hyperLinkText is undefined now, then $.trim(hyperLinkText) = "" is just a empty string
// so the reg below just work like the same as you wrote before /\{.+?\}/g
// and it can't work of course
// var Reg = new RegExp('\\{.+?\\' + $.trim(hyperLinkText) + '}','g');
// now we can try to match the text by hyperLinkId
// when it's HyperValues2[0] this reg work like /\{f19e7a87-3ae9-40d7-b81a-ad5eba8a2975.+?\}/g
// and will match {f19e7a87-3ae9-40d7-b81a-ad5eba8a2975, First Link}
// when it's HyperValues2[1] this reg work like /\{587d2209-fcf2-448d-b52d-7f0c93e59c8c.+?\}/g
// and will match {587d2209-fcf2-448d-b52d-7f0c93e59c8c, Second Link}
var Reg = new RegExp('\\{' + $.trim(hyperLinkId) + '.+?\\}','g');
description2 = description2.replace(Reg, '<a id= ' + hyperLinkId + ' ' + 'class="infoBoxPopup" data-toggle="modal" data-target="#moreInfoModal" href="#"> ' + hyperLinkText + '</a>');
});
$('#1').html(description1)
$('#2').html(description2)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="1"></div>
<div id="2"></div>
</body>