根據一條線:
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', '');
如何替代所有事件?
当前回答
我已经阅读了这个问题和答案,但我找不到一个合适的解决方案,虽然答案是相当有用的,我决定创建自己的解决方案从切割。
例如,对于搜索结果,我需要用相同的案例取代它,如果我处理内部HTML,我可以轻松地损害HTML标签(例如,在href属性中出现 hr)。
因此,我写的功能,以突出搜索结果在一个表,在那里表数据细胞可能有链接在内部,以及其他HTML标签。
我決定為所有人提供相同的問題的解決方案. 當然,你可以用它不僅為表,但為任何元素。
/* Iterate over table data cells to insert a highlight tag */
function highlightSearchResults(textFilter) {
textFilter = textFilter.toLowerCase().replace('<', '<').replace('>', '>');
let tds;
tb = document.getElementById('sometable'); //root element where to search
if (tb) {
tds = tb.getElementsByTagName("td"); //sub-elements where to make replacements
}
if (textFilter && tds) {
for (td of tds) {
//specify your span class or whatever you need before and after
td.innerHTML = insertCaseInsensitive(td.innerHTML, textFilter, '<span class="highlight">', '</span>');
}
}
}
/* Insert a highlight tag */
function insertCaseInsensitive(srcStr, lowerCaseFilter, before, after) {
let lowStr = srcStr.toLowerCase();
let flen = lowerCaseFilter.length;
let i = -1;
while ((i = lowStr.indexOf(lowerCaseFilter, i + 1)) != -1) {
if (insideTag(i, srcStr)) continue;
srcStr = srcStr.slice(0, i) + before + srcStr.slice(i, i+flen) + after + srcStr.slice(i+flen);
lowStr = srcStr.toLowerCase();
i += before.length + after.length;
}
return srcStr;
}
/* Check if an ocurrence is inside any tag by index */
function insideTag(si, s) {
let ahead = false;
let back = false;
for (let i = si; i < s.length; i++) {
if (s[i] == "<") {
break;
}
if (s[i] == ">") {
ahead = true;
break;
}
}
for (let i = si; i >= 0; i--) {
if (s[i] == ">") {
break;
}
if (s[i] == "<") {
back = true;
break;
}
}
return (ahead && back);
}
其他回答
我的实践,非常自我解释
function replaceAll(string, token, newtoken) {
if(token!=newtoken)
while(string.indexOf(token) > -1) {
string = string.replace(token, newtoken);
}
return string;
}
有一个方法可以使用新的替代All() 方法。
但您需要使用先进的浏览器或JavaScript运行时间环境。
您可以在这里查看浏览器兼容性。
可以用常见的表达方式实现这一点,有几种可以帮助某人:
var word = "this,\\ .is*a*test, '.and? / only / 'a \ test?";
var stri = "This is a test and only a test";
取代所有非阿尔法字符,
console.log(word.replace(/([^a-z])/g,' ').replace(/ +/g, ' '));
Result: [this is a test and only a test]
用一个空间替换多个连续空间,
console.log(stri.replace(/ +/g,' '));
Result: [This is a test and only a test]
取代所有 * 字符,
console.log(word.replace(/\*/g,''));
Result: [this,\ .isatest, '.and? / only / 'a test?]
取代问题标志(?)
console.log(word.replace(/\?/g,'#'));
Result: [this,\ .is*a*test, '.and# / only / 'a test#]
取代引用标志,
console.log(word.replace(/'/g,'#'));
Result: [this,\ .is*a*test, #.and? / only / #a test?]
要取代所有“字符”,
console.log(word.replace(/,/g,''));
Result: [this\ .is*a*test '.and? / only / 'a test?]
替换一个特定的词,
console.log(word.replace(/test/g,''));
Result: [this,\ .is*a*, '.and? / only / 'a ?]
取代Backslash。
console.log(word.replace(/\\/g,''));
Result: [this, .is*a*test, '.and? / only / 'a test?]
以替代前滑,
console.log(word.replace(/\//g,''));
Result: [this,\ .is*a*test, '.and? only 'a test?]
替换所有空间,
console.log(word.replace(/ /g,'#'));
Result: [this,\#.is*a*test,####'.and?#/#only#/#####'a##test?]
替换点,
console.log(word.replace(/\./g,'#'));
Result: [this,\ #is*a*test, '#and? / only / 'a test?]
这应该工作。
String.prototype.replaceAll = function (search, replacement) {
var str1 = this.replace(search, replacement);
var str2 = this;
while(str1 != str2) {
str2 = str1;
str1 = str1.replace(search, replacement);
}
return str1;
}
例子:
Console.log("Steve is the best character in Minecraft".replaceAll("Steve", "Alex"));
以前的答案太复杂了,只需使用替代功能如下:
str.replace(/your_regex_pattern/g, replacement_string);
例子:
var str = “测试 abc 测试 abc 测试 abc 测试 abc”; var res = str.replace(/[abc]+/g, ""); console.log(res);