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));

  • 相关阅读:
    centos下修改hosts文件以及生效命令
    CentOS 7 上安装(LAMP)服务 Linux,Apache,MySQL,PHP
    sqlserver下载地址及密匙
    npm/gulp/nodejs
    sp_addlinkedserver 跨服务器连接数据库查询
    sql使用临时表循环
    nodejs和npm
    扫描输入后自动定位到下一个输入框
    Unable to load the specified metadata resource
    mysql保存乱码(C#)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5167921.html
Copyright © 2011-2022 走看看