zoukankan      html  css  js  c++  java
  • [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));

  • 相关阅读:
    理解 Redis(3)
    理解 Redis(2)
    理解 Redis(1)
    git 的基本命令
    使用python实现计算器功能
    python函数说明内容格式错误
    python的小基础
    python去除读取文件中多余的空行
    数论-下属不可以和上司顶嘴!(可能是总结)
    其他-一大堆记录 (20 Dec
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5167921.html
Copyright © 2011-2022 走看看