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

  • 相关阅读:
    day10servlet编程
    day9http协议
    day8 服务器
    day4 DOM,BOM
    k均值
    asp.net mvc 下拉列表
    asp.net mvc  Ajax.BeginForm 异步上传图片的问题
    sqlserver 存储过程 游标实例
    中篇: php 微信支付 基于Thinkphp3.2开发
    下篇: php 微商城 基于Thinkphp3.2框架开发
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5167921.html
Copyright © 2011-2022 走看看