web front-end articles: JavaScript regular expression (2)

Method 1 test
the value specified in the search character string. Returns true or false. Value specified in the search character string. Returns true or false.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>text</title>
</head>
<body>
    <script type="text/javascript">
        var reg = /123/g;
        var str = 'hello123hello123';
        console.log(reg.test(str));//true
        console.log(reg.lastIndex);//8
        console.log(reg.test(str));//true
        console.log(reg.lastIndex);//16
        console.log(reg.test(str));//false
        console.log(reg.lastIndex);//0
        
    </script>
</body>
</html>
//使用了g修饰符的正则表达式,表示要记录每一次搜索的位置,接着使用test方法,每次开始搜索的位置都是上一次匹配的后一个位置。

3.2 exec method
a method for expression matching search character string in the positive. It returns an array in which to store the matching results. If a match is not found, the return value is null.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>exec</title>
</head>
<body>
    <script type="text/javascript">
        var str='abc';
        var reg1 = /x/;
        var reg2 = /a/;
        console.log(reg1.exec(str));//null
        console.log(reg2.exec(str));//array["a", index: 0, input: "xyz"]

    </script>
</body>
</html>

If the regular expression contains parentheses, the returned array will include multiple elements. First, the entire result of a successful match, followed by parentheses matching successful result, if there are multiple parentheses, the result of their match will be the success of the array elements

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>exec</title>
</head>
<body>
    <script type="text/javascript">
        var str='abcdabc';
        var reg1 = /(a)b(c)/;
        console.log(reg1.exec(str));//["abc", "a", "c", index: 0, input: "abcdabc", groups: undefined]
    </script>
</body>
</html>

Click to receive free information and courses

3 search method
search () method for substring search character string specified, retrieving or regular expression matching substring.

Return Value: stringObject regexp with the first substring that matches the starting position.

Note: If no matches any substring, or -1.

search () method does not perform a global match, it will ignore the signs g. It also ignores the lastIndex property of regexp and always be retrieved from the beginning of the string, which means that it always returns the position of the first match of stringObject.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>search</title>
</head>
<body>
    <script type="text/javascript">
        var str='abcdefgh';
        console.log(str.search('h'))//7
    </script>
</body>
</html>

Method 4match
match () method retrieves the value specified in string, or to find one or more regular expression matching. This method is similar to the indexOf () and lastIndexOf (), which returns a value but, instead of the string.

match method of a string object with regular exec method of the object is relatively similar: But if the regular expression with the g modifier, then the match will have different methods and the exec method: you can see all the results match the return of a successful match, but the exec method returns only one.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>match</title>
</head>
<body>
    <script type="text/javascript">
        var str = "abcd";
        var reg1 = /a/;
        var reg2 = /x/;
        console.log(str.match(reg1));//a
        console.log(str.match(reg2));//null

        var str2 = "abcabc";
        var reg3 = /a/g;
        console.log(str2.match(reg3));//['a','a']
    </script>
</body>
</html>

Click to receive free information and courses

5 replace method

replace () method for replacing some characters other characters, or alternatively a substring match the positive expression in using string.

Return Value: a new string is replaced with a first replacement regexp obtained after all of a match or matches.

StringObject the string replace () method is executed search and replace operation. It looks regexp substring matching the stringObject, followed by replacement to replace these substrings. If the flag has global regexp g, the replace () method will replace all substring matching. Otherwise, only replace the first matching substring.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>replace</title>
</head>
<body>
    <script type="text/javascript">
        var str = "aaa";
        console.log(str.replace('a', 'f'));//fbcdaa
    </script>
</body>
</html>

Click to receive free information and courses

6 split method
Split ( 'regular division string', 'maximum number of members of the returned array'); after returning the divided portions of an array

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>split</title>
</head>
<body>
    <script type="text/javascript">
        var str = "a,b,c,d,e,f";
        var res = str.split(",");//以逗号来分割字符串
        console.log(res);//["a", "b", "c", "d", "e", "f"]
        var str2 =  "a, b ,c,d";
        var res2 = str2.split(/, */);//以0或对个逗号空格来分割字符串
        console.log(res2);//["a", "b ", "c", "d"]
    </script>
</body>
</html>

7 Example:
extract subdomain from the URL

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>split</title>
</head>
<body>
    <script type="text/javascript">
          var url = "http://www.abc.com";
          var reg = /[^.]+/;//匹配除了.之外的其他字符
          var res = reg.exec(url)[0].substr(7);
          console.log(reg.exec(url));//["http://www", index: 0, input: "http://www.abc.com"]
          console.log(res);//www
    </script>
</body>
</html>

Guess you like

Origin blog.csdn.net/ITmiaomiao666/article/details/92009102