JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace variable's names with "[Michael and " + variable's name + "]" in the paragraph below:</p>
<p id="demo">int value1 = 0;
int value2=0;
long longvalue =0;
long verylongvalue=0;</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var var_type = ["long", "String"];
var var_type2 = "int";
var str = document.getElementById("demo").innerHTML;
var regexpString="(\\bint\\b";
var_type.map(function(type){
console.log(type);
regexpString += "\|\\b"+type+"\\b";
});
regexpString += ")\\s\*(\?\<varName\>\\w\+)";
//var original_regexp= "\\b"+var_type2+"\\b\\s\*(\?\<varName\>\\S\+)";
var regexp = new RegExp(regexpString, 'g');
var match = regexp.exec(str);
console.log(match);
if(match==null){
console.log('no match found');
}else{
var regexp2 = new RegExp("\\b"+match[2]+"\\b", 'g');
var res2 = str.replace(regexp2, "[Michael and $&]");
while(match !== null) {
console.log(match[2]);
match = regexp.exec(str);
if(match != null){
regexp2 = new RegExp("\\b"+match[2]+"\\b", 'g');
res2 = res2.replace(regexp2, "[Michael and $&]");
}
}
document.getElementById("demo").innerHTML = res2;
}
}
</script>
</body>
</html>