javascript之string正则式

1.search方法

复制代码
search() 返回第一个匹配的索引,未找到返回-1
javascript 复制代码
const str = "Hello World";
const index = str.search(/World/);
console.log(index); // 6
 

2 match方法

match() 用于在字符串中执行正则匹配,返回匹配结果的数组或null

javascript 复制代码
const str = "Hello World";
const result = str.match(/World/);
console.log(result); // ["World"]
 

3 replace方法

这个方法是用来替换匹配的子字符串,支持字符串或函数作为替换值

javascript 复制代码
const str = "Hello World";
const newStr = str.replace(/World/, "JavaScript");
console.log(newStr); // "Hello JavaScript"
 

4 split()方法

这个方法是用来分割字符串为数组

javascript 复制代码
const str = "a,b,c";
const arr = str.split(/,/);
console.log(arr); // ["a", "b", "c"]
 
相关推荐
raindayinrain1 分钟前
c++对象模型--内存布局,多态,运行时类型识别
开发语言·c++
镜子AI3 分钟前
镜子AI工程实践:用熵权法 + TOPSIS 做多方案选型排序(零依赖 Python)
开发语言·人工智能·python
估值探索者4 分钟前
【Python量化组合管理实战 02】钱该怎么分?等权、波动率倒数与动态评分三种配权实测
java·开发语言·python·接口·股票数据api接口
西索斯coding15 分钟前
Kimi K2.6 与 Qwen3.7 Max:跨文件修复评测与成本核算方法(2026)
开发语言·python·ai
从小白到软工26 分钟前
5-2 从查表到控件:造按钮
c语言·开发语言·数据结构·经验分享·ai编程
迪康Defender1 小时前
PHP 高危漏洞应急分析:从风险原理到端点侧纵深防护
运维·开发语言·安全·自动化·php·运维开发·远程工作
2601_966949651 小时前
为什么不应该把 QuantDash API Key 直接写进公开代码仓库?从 Git 提交历史理解凭证泄露风险
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
智_永无止境1 小时前
又找到一个纯Java的OCR项目,识别的格式还挺多
java·开发语言·ocr
小灰灰搞电子1 小时前
Rust+Slint 实现动态流光边框源码分享
开发语言·前端·rust·流光边框
包小黑1 小时前
Python 转 EXE (实用工具)
开发语言·python