我希望能夠使用某種函式替換字串的所有其他實體,也許可以這樣作業:
replace_every_other("i like ice cream","i","1")
// output => i l1ke ice cream
function replace_every_other(string, replace_string, replace_with) {
...
}
uj5u.com熱心網友回復:
如果要替換所有相同的字串,可以使用 regEx;
let str = "gg gamers, abc gg gamers, def gg gamers..";
let out = str.replace(/gg gamers/g, "gs gamers");
console.log(out)
uj5u.com熱心網友回復:
以下函式將替換所有其他特定字串。它不需要字串彼此相鄰。請注意,匹配區分大小寫。該函式的作業原理是在匹配的位置拆分輸入字串,然后通過更改插入的值重新插入匹配的部分。此外,該方法可以很容易地修改為通過改變模數操作來支持每三、四或 n:th 匹配的替換i % 2。
function replace_every_other(value, search, replacement) {
// Replace every odd search match by the replacement string.
// Return string.
// First, split to parts at the matched strings.
// This will remove all instances of the search string.
var parts = String(value).split(search)
// Handle empty strings strings with no matches.
if (parts.length < 2) {
return parts.join('')
}
// Insert between parts and
var replacedParts = []
for (var i = 0; i < parts.length; i = 1) {
// Add non-matched part
replacedParts.push(parts[i])
// Skip the end of value
if (i !== parts.length - 1) {
if (i % 2 === 0) {
replacedParts.push(search)
} else {
replacedParts.push(replacement)
}
}
}
return replacedParts.join('')
}
// Testing
console.log('("i like ice cream", "i", "1") => ',
replace_every_other('i like ice cream', 'i', '1'))
console.log('("i LIKE ice cream", "i", "1") => ',
replace_every_other('i LIKE ice cream', 'i', '1'))
uj5u.com熱心網友回復:
預先編輯(因為對要求進行了澄清更新)
以下split基于方法背后的主要思想是使用帶有單個捕獲組的正則運算式,該捕獲組針對要替換的字符/字串。因此,結果陣列在每次匹配時被拆分,但也包含作為單獨專案的匹配項。
其余部分是通過一個簡單的mapping來實作的,其中回傳值是通過對提供的nth替換進行相等和基于模數的計數來選擇的......
function replaceEveryNth(value, search, replacement, nthCount = 1) {
let matchCount = 0;
return String(value)
.split(
RegExp(`(${ search })`)
)
.map(str =>
((str === search) && ( matchCount % nthCount === 0))
? replacement
: str
)
.join('');
}
console.log(
'("i like ice cream", "i", "1") =>',
replaceEveryNth("i like ice cream", "i", "1")
);
console.log(
'("i like ice cream", "i", "1", 2) =>',
replaceEveryNth("i like ice cream", "i", "1", 2)
);
console.log(
'("i like ice cream", "i", "1", 3) =>',
replaceEveryNth("i like ice cream", "i", "1", 3)
);
console.log(
'("i like ice cream", "i", "1", 4) =>',
replaceEveryNth("i like ice cream", "i", "1", 4)
);
console.log('\n');
console.log(
'("i like ice cream less", "e", "y", 2) =>',
replaceEveryNth("i like ice cream less", "e", "y", 2)
);
console.log(
'("i like ice cream less", "e", "y", 3) =>',
replaceEveryNth("i like ice cream less", "e", "y", 3)
);
console.log('\n');
console.log(
'("foo bar baz bizz boozzz", "o", "0", 2) =>',
replaceEveryNth("foo bar baz bizz boozzz", "o", "0", 2)
);
console.log(
'("foo bar baz bizz boozzz", "z", "ss", 2) =>',
replaceEveryNth("foo bar baz bizz boozzz", "z", "ss", 2)
);
console.log(
'("foo bar baz bizz boozzz", "zz", "S", 2) =>',
replaceEveryNth("foo bar baz bizz boozzz", "zz", "S", 2)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
編輯結束
無需正則運算式即可完成此任務,String.prototype.replaceAll...
function replaceEveryOther(value, search, replacement) {
return String(value)
.replaceAll(
[search, search].join(''),
[search, replacement].join('')
);
}
console.log(
'("gg gamers foo buuuzz", "g", "s") =>',
replaceEveryOther("gg gamers foo buuuzz", "g", "s")
);
console.log(
'("gg gamers foo buuuzz", "o", "r") =>',
replaceEveryOther("gg gamers foo buuuzz", "o", "r")
);
console.log(
'("gg gamers foo buuuzz", "u", "n") =>',
replaceEveryOther("gg gamers foo buuuzz", "u", "n")
);
console.log(
'("gg gamers foo buuuuzz", "u", "n") =>',
replaceEveryOther("gg gamers foo buuuuzz", "u", "n")
);
console.log(
'("gg gamers foo buuuuzz", "uu", "U") =>',
replaceEveryOther("gg gamers foo buuuuzz", "uu", "U")
);
編輯
“有沒有辦法在 Node.js 中使用它?(因為 replaceAll 不可用)”
是的,當然……基于正則運算式……
function replaceEveryOther(value, search, replacement) {
return String(value)
.replace(
RegExp(`${ search }${ search }`, 'g'),
`${ search }${ replacement }`
);
}
console.log(
'("gg gamers foo buuuzz", "g", "s") =>',
replaceEveryOther("gg gamers foo buuuzz", "g", "s")
);
console.log(
'("gg gamers foo buuuzz", "o", "r") =>',
replaceEveryOther("gg gamers foo buuuzz", "o", "r")
);
console.log(
'("gg gamers foo buuuzz", "u", "n") =>',
replaceEveryOther("gg gamers foo buuuzz", "u", "n")
);
console.log(
'("gg gamers foo buuuuzz", "u", "n") =>',
replaceEveryOther("gg gamers foo buuuuzz", "u", "n")
);
console.log(
'("gg gamers foo buuuuzz", "uu", "U") =>',
replaceEveryOther("gg gamers foo buuuuzz", "uu", "U")
);
uj5u.com熱心網友回復:
將str_replace函式為你作業?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/341445.html
標籤:javascript 节点.js 正则表达式 细绳 代替
