JSFiddle - React, Tailwind, and code Playground

by Dzyann

HTML

<div>
 <label id="test"></label>
 </div>
<div>
 <label id="test1"></label>
 </div>
<div>
  <label id="test2"></label>
</div>

JavaScript

function getMatches(string, regex, index) {
  index || (index = 1); // default to the first capturing group
  var matches = [];
  var match;
  while (match = regex.exec(string)) {
    matches.push(match[index]);
  }
  return matches;
}

var myString = "JurisdictionInformations_1__Attachments_0__Id";
var myRegEx = /(_[0-9]*__)/g;

// Get an array containing the first capturing group for every match
var matches = getMatches(myString, myRegEx, 1);

for(var j = 0; j < matches.length; j++)
{
	var newString = myString.replace(matches[0], "_34234__");
}

// Log results
//document.write(matches.length + ' matches found: ' + JSON.stringify(matches))

 

$("#test").text(myString);
$("#test1").text(matches);
$("#test2").text(newString);