<!doctype html>
<meta charset= "UTF-8" >
<title>Document</title>
<script>
Remove duplicate elements from the array
The most important problem is the sorting of the array, the substitution of replace, the division, the return to the external
Randomly generated random numbers pressed into an empty array in arr
for (Var i=0,arr=[];i<10;i++) {
Arr.push (parseint (Math.random () * (12-8+1) +8));
}
Console.log (arr);
function Quchongfu (arr) {//local variable
return Arr.sort ()//Sort First
. Join (",,")//To,, to be spliced like a,,a,,b,,b,,b,,c,,d,,d
. Replace (/(^|,) ([^,]+) (,, \2) + (, |$)/g, "$1$2$4")
. Split (",,");
}
Arr=quchongfu (arr);//global variable
Console.log (arr);
var reg=/(^|,) ([^,]+) (,, \2) + (, |$)/g;
$ $4
. replace (/^,|,$/g, "") if you change the above sentence to this, it is to delete all duplicate elements
</script>
<body>
</body>
This article is relatively simple, convenient to view later
In addition, there is a little knowledge
var str= "Jack isn't a good man";//To output this sentence as "Jack5 is6 not7 A8 good9 Man10";
var i=5;
Str.replace (/\b ([a-za-z]+) \b/g,function (x) {return x+ (i++);}) The output is "Jack5 is6 not7 A8 good9 Man10"
where \b XXX \b This is the word boundary, used to match a word
Using regular method to match, simple and convenient
Remove repeated elements from the array with regular