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 @   东血  阅读(271)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

本站勉强运行 1780 天 21 小时 49 分 58 秒

目录导航
目录导航
ES6-字符串连接,startsWith与endsWith匹配
1.startsWith
1.1 个人理解
1.2 代码如下
2.endsWith
2.1 个人理解
2.2 代码如下
3.字符串连接
3.1 个人理解
3.2 代码如下
发布于 2020-05-22 00:25
点击右上角即可分享
微信分享提示