我试图替换字符串中的某些单词,因此我们有一个编码字符串:
%3Cscript%20async%20src%3D%22%2F%2Fpagead2.googlesyndication.com%2F%25dontreplace%25%2Fjs%2Fadsbygoogle.js%22%3E%3C%2Fscript%3E%0A%3Cins%20class%3D%E2%80%9C%25dontreplace%25%22%0Astyle%3D%22display%3Ainline-block%3Bwidth%3A300px%3Bheight%3A250px%22%0Adata-ad-client%3D%22ca-pub-xxxxxxxxxxxxxxxxxxxx%22%0Adata-ad-slot%3D%22yyyyyyyyyyyy%E2%80%9D%3E%0Aparams%20%3D%20%7B%0Apuid1%3A%E2%80%99%25replace1%25%E2%80%99%2C%0Apuid2%3A%E2%80%99%25replace2%25%27%2C%0Apuid3%3A%27%25replace3%25%27%0A%0A%7D
其中有几句话:
%25dontreplace%25
%25dontreplace%25
%25replace1%25
%25replace2%25
%25replace3%25
我需要将所有%25replaceX%25
替换为%replaceX%
,但不要触及所有%25dontreplace%25
(每个var x = "%3Cscript%20async%20src%3D%22%2F%2Fpagead2.googlesyndication.com%2F%25dontreplace%25%2Fjs%2Fadsbygoogle.js%22%3E%3C%2Fscript%3E%0A%3Cins%20class%3D%E2%80%9C%25dontreplace%25%22%0Astyle%3D%22display%3Ainline-block%3Bwidth%3A300px%3Bheight%3A250px%22%0Adata-ad-client%3D%22ca-pub-xxxxxxxxxxxxxxxxxxxx%22%0Adata-ad-slot%3D%22yyyyyyyyyyyy%E2%80%9D%3E%0Aparams%20%3D%20%7B%0Apuid1%3A%E2%80%99%25replace1%25%E2%80%99%2C%0Apuid2%3A%E2%80%99%25replace2%25%27%2C%0Apuid3%3A%27%25replace3%25%27%0A%0A%7D";
console.log(x);
var y = x.split(/(%25)/);
console.log(y);
var n = [];
for (var i = 0; i <= y.length; i++) {
if (y[i].indexOf('%25') >= 0) {
if (y[i + 1].indexOf('request.puid') >= 0) {
n.push('%'+y[i]+'%');
console.log(n);
}
} else {
if (y[i - 1] === undefined && y[i + 1] === '%25') {
n.push(y[i]);
console.log(n);
} else if (y[i - 1] === '%25' && y[i + 1] === undefined) {
n.push(y[i]);
console.log(n);
} else if (y[i - 1] === '%25' && y[i + 1] === '%25') {
n.push('%25'+y[i]+'%25');
console.log(n);
}
}
}
可以有不同的数量)
我试图将它拆分成阵列并做一些ifelse陈述,但它似乎不起作用,我认为它不是最优雅的:
@Override
public void onBindViewHolder(final AdapterHolder holder, final int position) {
final Item_GetMembers item_getMembers = CategoriesList.get(position);
//It means if your position is zero then zero position item will move at third position
if(position == 0)
moveItem(3, item_getMembers);
}
private void moveItem(int toItemPos, Item_GetMembers item)
{
for(int i = 0; i < CategoriesList.size(); i++){
if(toItemPos == i){
CategoriesList.add(i, item)
break;
}
}
notifyDataSetChanged();
}
&#13;
有没有一种优雅的方法来解决这个问题?
答案 0 :(得分:0)
您可以使用.replace
使用捕获组RegExp
。
我们使用的RegExp
是RegExp(/%25replace([0-9]*)%25/, 'g')
,它会找到与%25replaceX%25
匹配的字符串,而g
会找到所有匹配项。 ()
捕获数字,以便我们可以在替换文本中使用它。
这样的事情:
var str = "%3Cscript%20async%20src%3D%22%2F%2Fpagead2.googlesyndication.com%2F%25dontreplace%25%2Fjs%2Fadsbygoogle.js%22%3E%3C%2Fscript%3E%0A%3Cins%20class%3D%E2%80%9C%25dontreplace%25%22%0Astyle%3D%22display%3Ainline-block%3Bwidth%3A300px%3Bheight%3A250px%22%0Adata-ad-client%3D%22ca-pub-xxxxxxxxxxxxxxxxxxxx%22%0Adata-ad-slot%3D%22yyyyyyyyyyyy%E2%80%9D%3E%0Aparams%20%3D%20%7B%0Apuid1%3A%E2%80%99%25replace1%25%E2%80%99%2C%0Apuid2%3A%E2%80%99%25replace2%25%27%2C%0Apuid3%3A%27%25replace3%25%27%0A%0A%7D";
var newStr = str.replace(new RegExp(/%25replace([0-9]*)%25/, 'g'), "%replace$1%");
document.write(newStr)
&#13;