admin管理员组

文章数量:1403480

I want to replace the regex matched str with some other string after calling function. Can't find any method for the same ..

Consider

var str = "someRegex ajfgdgkjdljlgdgjlrm someRegeeeeex dfdsfj";
var re = /someReg(.*?)x/g;    
while ((m = re.exec(str)) != null) {
   // m= ["someRegex", "e"]
   // m= ["someRegeeeeex", "eeeee"]

// I WANT  TO REPLACE THE MATCHED WITH AFTER CALLING A FUNCTION 
// lets say someRegex with someRegex1 and someRegeeeeex with someRegeeeeex5
             }

Note : i don't want to use str replace since it will not be give me the correct solution. lets say replacing someRegex with someRegex1 and someRegeeeeex with someRegeeeeex5 . USING str.replace will replace first one again and again.

I want to replace the regex matched str with some other string after calling function. Can't find any method for the same ..

Consider

var str = "someRegex ajfgdgkjdljlgdgjlrm someRegeeeeex dfdsfj";
var re = /someReg(.*?)x/g;    
while ((m = re.exec(str)) != null) {
   // m= ["someRegex", "e"]
   // m= ["someRegeeeeex", "eeeee"]

// I WANT  TO REPLACE THE MATCHED WITH AFTER CALLING A FUNCTION 
// lets say someRegex with someRegex1 and someRegeeeeex with someRegeeeeex5
             }

Note : i don't want to use str replace since it will not be give me the correct solution. lets say replacing someRegex with someRegex1 and someRegeeeeex with someRegeeeeex5 . USING str.replace will replace first one again and again.

Share Improve this question asked May 23, 2014 at 15:51 shrwshrw 1,8016 gold badges31 silver badges53 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

It looks like you're trying to append the length of the group's value to the match. If that's the case, replace can be used with a function:

var str = "someRegex ajfgdgkjdljlgdgjlrm someRegeeeeex dfdsfj";
var re = /someReg(.*?)x/g; 
var result = str.replace(re, function(match, group) {
  return match + group.length;
});
console.log(result);

Refer to Specifying a function as a parameter for the String.prototype.replace method.

First, use a while to loop on your string.

Then inside the loop use replace to update the founded string.

var str = 'one two [%test1%] try some test [%test2%]';
var regex = /\[%(.*?)\%]/g;
    
function populateStudioMacro() {
  var result;
  while((result = regex.exec(str)) !== null) {
  	// match found
  	document.body.innerHTML += 'Found<br>';
  	document.body.innerHTML += result[0] + '<br>';
  	document.body.innerHTML += result[1] + '<br>';

	// replace string matched
	str = str.replace(result[0], ' =>new string<= ');
  }
  document.body.innerHTML += str;
}

populateStudioMacro();

USING str.replace will replace first one again and again.

This is only true if you pass the /g modifier. Not sure what your actual data is so that may or may not be relevant.

Again without knowing your actual data, I think I'd use match() here instead of exec() and reconstruct the string from matches. E.g.

var str = "someRegex ajfgdgkjdljlgdgjlrm someRegeeeeex dfdsfj";
var re = /someReg(.*?)x/g;
str.match(re);

produces

["someRegex", "someRegeeeeex"]

本文标签: javascript regex exec and replaceStack Overflow