ES6-字符串连接,startsWith与endsWith匹配

1.startsWith

1.1 个人理解

  • 看最前面的字符串是否与给出的字符串匹配

1.2 代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>startsWith</title>
    <script>
        let str = 'http://www.adsd.com/';
        if (str.startsWith('http://')) {
            alert('普通网址');
        } else if (str.startsWith('https://')) {
            alert('加密网址');
        } else if (str.startsWith('git://')) {
            alert('git网址');
        }
    </script>
</head>
<body>
</body>
</html>

2.endsWith

2.1 个人理解

  • 看最后面的字符串是否与给出的字符串匹配

2.2 代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>endsWith</title>
    <script>
        let str = '123313@qq.com';
        if (str.endsWith('@qq.com')) {
            alert('QQ邮箱');
        } else if (str.endsWith('@163.com')) {
            alert('网易云邮箱');
        } else {
            alert('其他邮箱');
        }
    </script>
</head>
<body>
</body>
</html>

3.字符串连接

3.1 个人理解

  • 直接把东西塞到字符串里面 ${东西} 可以折行

3.2 代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字符模板化</title>
    <script>
        let title = "标题";
        let content = "内容";
        let strone = '<div>\
            <h1>' + title + '</h1>\
            <p>' + content + '</p>\
            </div>';
        alert(strone);
        let strtwo = `<div>
            <h1>${title}</h1>
            <p>${content}</p>
            </div>`
        alert(strtwo);
    </script>
</head>
<body>
</body>
</html>
posted @ 2020-05-22 00:25  东血  阅读(253)  评论(0编辑  收藏  举报

载入天数...载入时分秒...