根據一條線:
s = "Test abc test test abc test test test abc test test abc";
这似乎只是在上面的行中删除ABC的第一次出现:
s = s.replace('abc', '');
如何替代所有事件?
根據一條線:
s = "Test abc test test abc test test test abc test test abc";
这似乎只是在上面的行中删除ABC的第一次出现:
s = s.replace('abc', '');
如何替代所有事件?
当前回答
我喜欢这个方法(看起来有点干净):
text = text.replace(new RegExp("cat","g"), "dog");
其他回答
在JavaScript中使用RegExp可以为您完成工作,只需在下面的代码中做一些类似的事情,不要忘记 /g 之后是全球性的:
var str ="Test abc test test abc test test test abc test test abc";
str = str.replace(/abc/g, '');
如果你想重复使用,创建一个功能来为你做到这一点,但它不推荐,因为它只是一个线功能。
String.prototype.replaceAll = String.prototype.replaceAll || function(string, replaced) {
return this.replace(new RegExp(string, 'g'), replaced);
};
并简单地使用它在你的代码上和上如下:
var str ="Test abc test test abc test test test abc test test abc";
str = str.replaceAll('abc', '');
但是,正如我之前提到的那样,它不会在写字或性能方面产生巨大的差异. 只有加密功能可能会影响长线上的某些更快的性能,如果您想要重新使用,则是DRY代码的良好实践。
String.prototype.replace 所有()
如果你不想处理替代() + RegExp。
但是,如果浏览器在2020年之前?
我推荐的替代All polyfill的选项:
替代All polyfill (与全球旗帜错误) (更多原则版)
if (!String.prototype.replaceAll) { // Check if the native function not exist
Object.defineProperty(String.prototype, 'replaceAll', { // Define replaceAll as a prototype for (Mother/Any) String
configurable: true, writable: true, enumerable: false, // Editable & non-enumerable property (As it should be)
value: function(search, replace) { // Set the function by closest input names (For good info in consoles)
return this.replace( // Using native String.prototype.replace()
Object.prototype.toString.call(search) === '[object RegExp]' // IsRegExp?
? search.global // Is the RegEx global?
? search // So pass it
: function(){throw new TypeError('replaceAll called with a non-global RegExp argument')}() // If not throw an error
: RegExp(String(search).replace(/[.^$*+?()[{|\\]/g, "\\$&"), "g"), // Replace all reserved characters with '\' then make a global 'g' RegExp
replace); // passing second argument
}
});
}
替代All polyfill (With handling global-flag missing by itself) (我的第一个偏好) - 为什么?
if (!String.prototype.replaceAll) { // Check if the native function not exist
Object.defineProperty(String.prototype, 'replaceAll', { // Define replaceAll as a prototype for (Mother/Any) String
configurable: true, writable: true, enumerable: false, // Editable & non-enumerable property (As it should be)
value: function(search, replace) { // Set the function by closest input names (For good info in consoles)
return this.replace( // Using native String.prototype.replace()
Object.prototype.toString.call(search) === '[object RegExp]' // IsRegExp?
? search.global // Is the RegEx global?
? search // So pass it
: RegExp(search.source, /\/([a-z]*)$/.exec(search.toString())[1] + 'g') // If not, make a global clone from the RegEx
: RegExp(String(search).replace(/[.^$*+?()[{|\\]/g, "\\$&"), "g"), // Replace all reserved characters with '\' then make a global 'g' RegExp
replace); // passing second argument
}
});
}
小型(我的第一个偏好):
if(!String.prototype.replaceAll){Object.defineProperty(String.prototype,'replaceAll',{configurable:!0,writable:!0,enumerable:!1,value:function(search,replace){return this.replace(Object.prototype.toString.call(search)==='[object RegExp]'?search.global?search:RegExp(search.source,/\/([a-z]*)$/.exec(search.toString())[1]+'g'):RegExp(String(search).replace(/[.^$*+?()[{|\\]/g,"\\$&"),"g"),replace)}})}
其他方法的聚合物分配
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function(search, replace) { // <-- Naive method for assignment
// ... (Polyfill code Here)
}
}
for (var k in 'hi') console.log(k);
// 0
// 1
// replaceAll <-- ?
非常可靠,但重
事实上,我提出的选项有点乐观,正如我们信任环境(浏览器和Node.js),它肯定是2012年至2021年左右。
此分類上一篇: HTTPS://polyfill.io
特别是替代:
<script src="https://polyfill.io/v3/polyfill.min.js?features=String.prototype.replaceAll"></script>
对抗全球常规表达:
anotherString = someString.replace(/cat/g, 'dog');
看看这个答案,也许它会帮助,我在我的项目中使用它。
function replaceAll(searchString, replaceString, str) {
return str.split(searchString).join(replaceString);
}
replaceAll('abc', '',"Test abc test test abc test test test abc test test abc" ); // "Test test test test test test test test "
您可以在没有Regex的情况下做到这一点,但如果替代文本包含搜索文本,则要小心。
吉。
replaceAll("nihIaohi", "hI", "hIcIaO", true)
因此,这里是一个合适的替代All 选项,包括字符串的原型:
function replaceAll(str, find, newToken, ignoreCase)
{
let i = -1;
if (!str)
{
// Instead of throwing, act as COALESCE if find == null/empty and str == null
if ((str == null) && (find == null))
return newToken;
return str;
}
if (!find) // sanity check
return str;
ignoreCase = ignoreCase || false;
find = ignoreCase ? find.toLowerCase() : find;
while ((
i = (ignoreCase ? str.toLowerCase() : str).indexOf(
find, i >= 0 ? i + newToken.length : 0
)) !== -1
)
{
str = str.substring(0, i) +
newToken +
str.substring(i + find.length);
} // Whend
return str;
}
或者,如果你想有一个字符串原型功能:
String.prototype.replaceAll = function (find, replace) {
let str = this;
let i = -1;
if (!str)
{
// Instead of throwing, act as COALESCE if find == null/empty and str == null
if ((str == null) && (find == null))
return newToken;
return str;
}
if (!find) // sanity check
return str;
ignoreCase = ignoreCase || false;
find = ignoreCase ? find.toLowerCase() : find;
while ((
i = (ignoreCase ? str.toLowerCase() : str).indexOf(
find, i >= 0 ? i + newToken.length : 0
)) !== -1
)
{
str = str.substring(0, i) +
newToken +
str.substring(i + find.length);
} // Whend
return str;
};