ES6—字符串变化

一、ES6 新增方法:startsWith()、endsWith()、repeat()

1、startsWith()

判断一个字符串是否以一个指定的子字符串开头,根据结果返回true/false

javascript 复制代码
const str1='welcome to BeiJing'
console.log(str1.startsWith('a'))   // false
console.log(str1.startsWith('wel')) // true

2、endsWith()

判断一个字符串是否以一个指定的子字符串结尾,根据结果返回true/false

javascript 复制代码
const str1='welcome to BeiJing'
console.log(str1.endsWith('a'))     // false
console.log(str1.endsWith('ing'))   // true

3、repeat()

构造并返回一个新字符串,其中包含指定数量的所调用的字符串副本,这些副本连接在一起

javascript 复制代码
const str2='happy!'
console.log(`I'm so ${str2.repeat(3)}!!`)  //I'm so happy!happy!happy!!!

二、match()方法

检索字符串与正则表达式进行匹配的结果

javascript 复制代码
const paragraph='The quick brown fox jumps over the lazy dog. It barked.'
const reg=/[^(a-z)]/g
const found=paragraph.match(reg)
console.log(found)

三、search()方法

用于在 String 对象中执行正则表达式的搜索,寻找匹配项

javascript 复制代码
const paragraph='The quick brown fox jumps over the lazy dog. It barked.'
const reg2=/[^\w\s]/g
console.log(paragraph.search(reg2)) // 43
console.log(paragraph.indexOf('.')) // 43
相关推荐
AC赳赳老秦8 分钟前
Prometheus + DeepSeek:自动生成巡检脚本与告警规则配置实战
前端·javascript·爬虫·搜索引擎·prometheus·easyui·deepseek
接着奏乐接着舞。10 分钟前
前端大数据渲染性能优化:Web Worker + 分片处理 + 渐进式渲染
大数据·前端·性能优化
Beginner x_u18 分钟前
CSS 中的高度、滚动与溢出:从 height 到 overflow 的完整理解
前端·css·overflow·min-height
vx1_Biye_Design22 分钟前
基于web的物流管理系统的设计与实现-计算机毕业设计源码44333
java·前端·spring boot·spring·eclipse·tomcat·maven
tqs_1234530 分钟前
倒排索引数据结构
java·前端·算法
a程序小傲33 分钟前
听说前端又死了?
开发语言·前端·mysql·算法·postgresql·深度优先
Yan.love1 小时前
【CSS-布局】终极方案:Flexbox 与 Grid 的“降维打击”
前端·css
请叫我聪明鸭1 小时前
基于 marked.js 的扩展机制,创建一个自定义的块级容器扩展,让内容渲染为<div>标签而非默认的<p>标签
开发语言·前端·javascript·vue.js·ecmascript·marked·marked.js插件
悟能不能悟1 小时前
Gson bean getxxx,怎么才能返回给前端
java·前端
2501_944711431 小时前
前端向架构突围系列 - 工程化(五):企业级脚手架的设计与落地
前端·架构