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

原文地址:https://www.cnblogs.com/Answer1215/p/5174320.html