[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 Patternstitle>
  <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 =>
      `${str}`
    );
}
output(str, regex, document.querySelector('pre'));

 

[Regular Expressions] Find Plain Text Patterns_第1张图片

你可能感兴趣的:([Regular Expressions] Find Plain Text Patterns)