例1
代码如下 |
复制代码 |
String.prototype.repeatOpt = function () {
var str = this + "",objStr = "";
for (var i = 0; i < this.length; i++) {
var s = str[i];
var newStr = str.replace(s, '');
var j = newStr.indexOf(s);
if (j == -1) {
objStr += s;
}
}
return objStr;
}
alert("1,2,2,3,4,4,5,6".repeatOpt());
|
例2
北京A,北京A,南京B,南京B,上海,天津,天健,上海
代码如下 |
复制代码 |
<script type="text/javascript">
var str = "北京A,北京A,南京B,南京B,上海,天津,天健,上海";
var arr = [];
str = str.replace(/([^,]*)/g, function($0, $1, i){
if(str.indexOf($1) == i) arr[arr.length] = $1;
});
document.write(arr.join(','));
</script> |
例3
代码如下 |
复制代码 |
function filterRepeatStr(str){
var ar2 = str.split(",");
var array = new Array();
var j=0
for(var i=0;i<ar2.length;i++){
if((array == "" || array.toString().match(new RegExp(ar2[i],"g")) == null)&&ar2[i]!=""){
array[j] =ar2[i];
array.sort();
j++;
}
}
return array.toString();
}
|
|