[Regular Expressions] Introduction

复制代码
var str = "Is this This?";

//var regex = new RegExp("is", "gi");
var regex = /is/gi;

//console.log(regex.test(str));
console.log(regex.exec(str)); //["Is", index: 0, input: "Is this This?"]
console.log(regex.exec(str)); //["is", index: 5, input: "Is this This?"]
console.log(regex.exec(str)); //["is", index: 10, input: "Is this This?"]
console.log(regex.exec(str)); //null

console.log(str.match(regex)); //["Is", "is", "is"]

console.log(str.replace(regex, "XX")); //"XX thXX ThXX?"

console.log(str.search(regex)); // 0, return the first index that found
复制代码

 

-----------------------------

App:

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Javascript Regular Expressions: Introduction</title>
  <style>
    pre {
      line-height: 2;
    }

    span {
      background-color: #eee;
      padding: 1px;
      outline: 1px solid #999;
    }

  </style>
</head>
<body>
  <pre></pre>
</body>
</html>
复制代码
复制代码
'use strict';

const output = (str, regex, target) => {
  target.innerHTML =
    str.replace(regex, str => `<span>${str}</span>`);
}

var str = `Is this This?`;

//var regex = new RegExp("is", "g");
var regex = /is/gi;

output(str, regex, document.querySelector('pre'))

// console.log(regex.test(str));
// console.log(regex.exec(str));
// console.log(regex.exec(str));
// console.log(regex.exec(str));
// console.log(regex.exec(str));
// console.log(str.match(regex));
// console.log(str.replace(regex, str => "XX"));
// console.log(str.search(regex));
复制代码

posted @   Zhentiw  阅读(182)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示