开发者

How can I replace a match only at a certain position inside the string?

开发者 https://www.devze.com 2023-03-23 11:08 出处:网络
So, I have a function which has two params: string and match index to replac开发者_如何学Ce and i need to replace only match with that index. How can i do that?

So, I have a function which has two params: string and match index to replac开发者_如何学Ce 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


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


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/


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)...


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]


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"

0

精彩评论

暂无评论...
验证码 换一张
取 消