Some simple applications of regular expressions

1. Match 11 mobile phone numbers with the first letter of 1

^1\d{10}$

2. Matching name, 3-6 characters in Chinese

[\u4e00-\u9fa5]{3,6}

3. Matching password must be 6-16 characters, and can only contain numbers, letters and underscores:

$\w{6-16}^

4. Matching mailbox

^\w+@\w+(\.\w+){1,2}

5. Match the number of the seat

^\d{1,3}-\d{4,8}$

6. Match a positive number

^\d+(\.\d+)?$

7. Match a decimal

^-?\d+\.\d+

8. Match an integer

^-?\d+$

 

9. Write a regular expression to match a string to get the number of matches and the results

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var reg = /\d{3}/g;
        var s = "1234a;sdfja;sd234lkfjasldkfj";
        var n = 0;
        while(result = reg.exec(s)){
            n++;
            console.log(result[0]);
        }
        console.log(`matching ${n}second`);
    </script>
</body>

</html>
index.html

10. Get the number of Chinese characters in a string:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var reg = /[\u4e00-\u9fa5]/g;
        var s = "1234a;s Water and electricity expenses 234 lkfjasldkfj";
        var n = 0;
        while(reg.test(s)){
            n++;
        }
        console.log(n);
    </script>
</body>

</html>
index.html

11. Filter sensitive words and replace sensitive words in a string with three asterisks:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var senWords = ["Xiao Ming", "Xiaohong", "Xiao Wang"];
        //Replace the sensitive words in the string with the specified string
        function filterSensitiveWords(s, repStr){
            var reg = new RegExp(`(${senWords.join("|")})+`,"g");
            return s.replace(reg, repStr);
        }
        console.log(filterSensitiveWords("Xiao Ming, Xiao Hong and Xiao Wang go to school. Xiao Ming is naughty and mischievous in class. Xiao Hong listens to the class carefully. Xiao Wang is in a trance", "***"));
    </script>
</body>

</html>
index.html

12. Separate a string of numbers with commas every three numbers from the back to the front:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var s = "123123123";
        //It's not a character boundary. It's followed by three numbers from 1 to many until the end. Global matching
        var reg = /\B(?=(\d{3})+$)/g;
        console.log(s.replace(reg, ","));
    </script>
</body>

</html>
index.html

Results:

 

13. Judge the strength of password. It is required that lowercase letters, uppercase letters, numbers and special characters (! @ #):

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //Determine the strength of the password. It is required that lowercase letters, uppercase letters, numbers and special characters appear in the password(!@#_,.)
        var s = "123asdf&AAA(";
        var reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[(!#_,.)]).{6,12}$/;
        console.log(reg.test(s));
    </script>
</body>

</html>
index.html

Keywords: Javascript Mobile

Added by Nameless12 on Thu, 23 Apr 2020 17:32:16 +0300