我有一些字符串,其中包含一些重复属性,我必须将其替换为如下所示:
我有字符串
".lf-form-container{font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;background-color:rgb(217,217,217) !important;}"
我想成功
".lf-form-container{font-family:'Helvetica Neue',sans-serif !important;background-color:rgb(217,217,217) !important;}"
通过使用javascript,任何人都可以帮助我。
答案 0 :(得分:2)
试试这个:
var string = ".lf-form-container{font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;background-color:rgb(217,217,217) !important;}",
declarations = string.match(/\{(.+)\}/)[1];
string = string.replace(declarations, [...new Set(declarations.split(";"))].join(";"));
这可以通过在分号周围分割字符串,通过使用Set
过滤生成的数组以获取唯一值,并再次使用分号连接它。请注意,这不是删除重复子串的一般解决方案;它只适用于以分号分隔的重复项。
如果ES6不可用,这是一个ES5解决方案:
string = string.replace(declarations, declarations.split(";").filter(function(value, index, self) {
return self.indexOf(value) === index;
}).join(";"));
答案 1 :(得分:0)
考虑到所有属性与;
相结合,您可以使用字符串.split()
和.join()
方法来获得所需的结果;
var demo = "font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;background-color:rgb(217,217,217) !important;"
var res = str_text.split(';').filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join(';');
var demo = "font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;background-color:rgb(217,217,217) !important;"
var res=demo.split(';').filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join(';');
console.log(res)
答案 2 :(得分:0)
您可以通过string
将此array
设为split
并删除重复的数组,如下所示:
var string = "font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;background-color:rgb(217,217,217) !important;";
var array = string.split(';');
var result = [];
$.each(array, function(i, el) {
if ($.inArray(el, result) === -1) result.push(el);
});
console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 3 :(得分:0)
试试这个
var str = "font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;background-color:rgb(217,217,217) !important;";
var arrStr = str.split(";");
var uniq = arrStr.reduce(function(a,b){
if (a.indexOf(b) < 0 ) a.push(b);
return a;
},[]);
console.log(uniq.join(";"));
&#13;
答案 4 :(得分:0)
您似乎试图仅保留首次出现的属性,请尝试此方法
var input = "font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;font-family:'Helvetica Neue',sans-serif !important;background-color:rgb(217,217,217) !important;";
function retainOnlyFirstOccurence(input) {
var propMap = input.split(";").reduce(function(a, b) {
if (b.length == 0) return a;
var splitB = b.split(":");
if (typeof a == "object") {
if (!a[splitB[0].trim()]) {
a[splitB[0].trim()] = splitB[1];
}
} else {
var splitA = a.split(":");
var splitb = b.split(":");
a = {};
if (!a[splitA[0].trim()]) {
a[splitA[0].trim()] = splitA[1];
}
if (!a[splitB[0].trim()]) {
a[splitB[0].trim()] = splitB[1];
}
}
return a;
});
return Object.keys( propMap ).map( function( key ){
return key + ":" + propMap[ key ];
}).join( ";" );
};
console.log( retainOnlyFirstOccurence( input ) );
逻辑
split
by;获得所有关键值reduce
获取不会覆盖属性的proMap
。map
再次将propMap
转换为键值join
加入所有关键值;