9

So, I have a function which has two params: string and match index to replace and i need to replace only match with that index. How can i do that?

Example:

replace('a_a_a_a_a', 1)

Result:

a__a_a_a
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
ruslan.savenok
  • 105
  • 1
  • 1
  • 4
  • 1
    Try using regexp, look at this : [Replacing the nth instance of a regex match in javascript][1] [1]: http://stackoverflow.com/questions/36183/replacing-the-nth-instance-of-a-regex-match-in-javascript – Igoris Azanovas Jul 27 '11 at 11:23
  • 3
    can you provide another example of input parameters so the required functionality is clearer? – BonyT Jul 27 '11 at 11:23

5 Answers5

12

Could look like:

var mystr = 'a_a_a_a_a';

function replaceIndex(string, at, repl) {
   return string.replace(/\S/g, function(match, i) {
        if( i === at ) return repl;

        return match;
    });
}

replaceIndex(mystr, 2, '_');

The above code makes usage of the fact, that .replace() can take a funarg (functional argument) as second parameter. That callback is passed in the current match of the matched regexp pattern and the index from that match (along with some others which we are not interested in here). Now that is all information we need to accomplish your desired result. We write a little function wish takes three arguments:

  • the string to modify
  • the index we wish to change
  • the replacement character for that position
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 7
    Close, but I think the `index` the OP is interested in is the `nth` occurrence of the pattern, not the string position of the match. – ridgerunner Jul 27 '11 at 12:27
9

For those like me who find regex to be cryptic, here is "pure JavaScript" way as well:

function CustomReplace(strData, strTextToReplace, strReplaceWith, replaceAt) {
    var index = strData.indexOf(strTextToReplace);
    for (var i = 1; i < replaceAt; i++)
        index = strData.indexOf(strTextToReplace, index + 1);
    if (index >= 0)
        return strData.substr(0, index) + strReplaceWith + strData.substr(index + strTextToReplace.length, strData.length);
    return strData;
}

Usage:

var mystr = 'a_a_a_a_a';
var newstr = CustomReplace(mystr, "_", "__", 2); //replace the second appearance

Live test case: http://jsfiddle.net/tXx5n/2/

Nathan Long
  • 122,748
  • 97
  • 336
  • 451
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

Javascript match returns an array, in case of multiple matches, so you could so something like this:

var string = "....";
var patt1 = /..../gi;
var results = string.match(patt1);
var newString = results.splice(i, i).join();

Instead of using a match, you could use split instead in your specific case:

var results = string.split("_");
var newString = results.splice(i, i).join("_");

It depends on how your input data can vary and where you need to do the split/match (that is why I did not specify any regex above, the split example is complete)...

sergio
  • 68,819
  • 11
  • 102
  • 123
  • Sorry, I added a bad example, i need to modify my match like that — before: im_1_2_3_4, after im_1_%2%_3_4. Function still the same, detect all numbers in string and replace match with my index. – ruslan.savenok Jul 27 '11 at 11:45
0

You need to use the function parameter of the replace function, and also maintain an index (i) of matches.

function replaceAt(string, replaceThis, replaceWith, at) {
  let i = 0;
  if (Array.isArray(at)) {
    return string.replace(replaceThis, (match, offset) => at.includes(i++) ? replaceWith : match)
  } else if (Number.isInteger(at)) {
    return string.replace(replaceThis, (match, offset) => i++ === at ? replaceWith : match)
  } else {
    return string;
  }
}
// usage:
console.log(replaceAt("aaaaabcde", /a/g, ".", [2, 0])); // ".a.aaabcde"
console.log(replaceAt("aaaaabcde", /a/g, ".", 2)); // "aa.aabcde"

String.prototype.replaceAt = function(replaceThis, replaceWith, at) {
  let string = this, i = 0;
  if (Array.isArray(at)) { // e.g. console.log("aaaaabcde".replaceAt(/a/g, ".", 2)); => "aa.bcde"
    return string.replace(replaceThis, (match, offset) => at.includes(offset) ? replaceWith : match)
  } else if (Number.isInteger(at)) { //e.g. console.log("aaaaabcde".replaceAt(/a/g, ".", [2, 0])); => ".a.bcde"
    return string.replace(replaceThis, (match, offset) => offset === at ? replaceWith : match)
  } else {
    return string;
  }
}
// usage:
console.log("aaaaabcde".replaceAt(/a/g, ".", [2, 0])); // ".a.aaabcde"
console.log("aaaaabcde".replaceAt(/a/g, ".", 2)); // "aa.aabcde"

function replaceExceptAt(string, replaceThis, replaceWith, at) {
  let i = 0;
  if (Array.isArray(at)) { // e.g. console.log(replaceExceptAt("aaaaabcde", /a/g, ".", 2); => "..a..bcde"
    return string.replace(replaceThis, (match, offset) => !at.includes(i++) ? replaceWith : match)
  } else if (Number.isInteger(at)) { //e.g. console.log(replaceExceptAt("aaaaabcde", /a/g, ".", [2, 0])); => "a.a..bcde"
    return string.replace(replaceThis, (match, offset) => i++ !== at ? replaceWith : match)
  } else {
    return string;
  }
}
// usage:
console.log(replaceExceptAt("aaaaabcde", /a/g, ".", [2, 0])); // "a.a..bcde"
console.log(replaceExceptAt("aaaaabcde", /a/g, ".", 2)); // "..a..bcde"

String.prototype.replaceExceptAt = function(replaceThis, replaceWith, at) {
  let string = this, i = 0;
  if (Array.isArray(at)) { // e.g. console.log("aaaaabcde".replaceExceptAt(/a/g, ".", 2); => "..a..bcde"
    //return string.replace(replaceThis, (match, offset) => !at.includes(offset) ? replaceWith : match)
    return string.replace(replaceThis, (match, offset) => !at.includes(i++) ? replaceWith : match)
  } else if (Number.isInteger(at)) { //e.g. console.log(replaceAt("aaaaabcde", /a/g, ".", [2, 0])); => "a.a..bcde"
    return string.replace(replaceThis, (match, offset) => i++ !== at ? replaceWith : match)
  } else {
    return string;
  }
}
// usage:
console.log("aaaaabcde".replaceExceptAt(/a/g, ".", [2, 0])); // "a.a..bcde"
console.log("aaaaabcde".replaceExceptAt(/a/g, ".", 2)); // "..a..bcde"
Tigregalis
  • 607
  • 3
  • 11
0

Use regex match with g flag var str="test string".match(/t/g) output would be array of matched strings in this case "t" [t,t,t]

rajani
  • 66
  • 3