Remove repeated elements from the array with regular

Source: Internet
Author: User

<!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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.