zoukankan      html  css  js  c++  java
  • [Regular Expressions] Find Plain Text Patterns

    The simplest use of Regular Expressions is to find a plain text pattern. In this lesson we'll look at at finding plain text patterns as well as using the metacharacter "." and how to escape a metacharacter.

     
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Javascript Regular Expressions: Find Plain Text Patterns</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';
    
    
    var str = `Cat
    sat on
    the hat.`;
    var regex = /.../g  //any two charaters follow by a '.'
    
    /**
     * @param  String str
     * @param  RegExp regex
     * @param  HTMLElement target
     */
    const output = (str, regex, target) => {
      target.innerHTML =
        str.replace(regex, str =>
          `<span>${str}</span>`
        );
    }
    output(str, regex, document.querySelector('pre'));

  • 相关阅读:
    【leetcode】二叉搜索树的最近公共祖先
    052-12
    052-11
    052-10
    052-9
    052-8
    052-6
    052-5
    052-3
    052-2
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5174320.html
Copyright © 2011-2022 走看看