Regular Expression Backreferences provide us a method to match a previously captured pattern a second time.

For example we have an string, and we want to any word which appear twice at the same time:

var str = "Time is the the most important thing thing."
var regex = /(the|thing)\s?/g;

Now it catch 'the' & 'thing', but we only want the first appear one.

var regex = /(the|thing)\s?(?=\1)/g;

--------------

Code:

var str = `Time is the the most important thing thing.`;
var regex = /(the|thing)\s?(?=\1)/g; console.log(str.replace(regex, '')); /*
"Time is the most important thing."
*/

And of course, we can do better:

var regex = /(\w+)\s?(?=\1)/g;

----------------------------

Also we can use this tech to extract the html content:

var str = `<b>Bold</b><i>italics</i>`;

So, first we want to match <></>:

So, '\1' means capture the first group. '(?=)' means only the first appear one.

var regex = /<(\w+)><\/\1>/g;

Then we want to add secod catch group of the content:

var regex = /<(\w+)>(.*)<\/\1>/g;
var str = `<b>Bold</b><i>italics</i>`;
var regex = /<(\w+)>(.*)<\/\1>/g; console.log(str.replace(regex, '$2\n')); /*
"Bold
italics
"
*/

[Regular Expressions] Match the Same String Twice的更多相关文章

  1. [Regular Expressions] Match the Start and End of a Line

    We can use: ^: match the beginning $: match the end Let's say we have the string like the following: ...

  2. PCRE Perl Compatible Regular Expressions Learning

    catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...

  3. 8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

  4. Regular Expressions --正则表达式官方教程

    http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...

  5. Regular Expressions in Grep Command with 10 Examples --reference

    Regular expressions are used to search and manipulate the text, based on the patterns. Most of the L ...

  6. Introducing Regular Expressions 学习笔记

    Introducing Regular Expressions 读书笔记 工具: regexbuddy:http://download.csdn.net/tag/regexbuddy%E7%A0%B4 ...

  7. [转]8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

  8. 正则表达式(Regular expressions)使用笔记

    Regular expressions are a powerful language for matching text patterns. This page gives a basic intr ...

  9. [Python] Regular Expressions

    1. regular expression Regular expression is a special sequence of characters that helps you match or ...

随机推荐

  1. iphone开发 IOS 组织架构图

    转载自 :http://blog.csdn.net/mashi321323/article/details/18267719   登录|注册     mashi321323的专栏       目录视图 ...

  2. 【转】Difference between Point-To-Point and Publish/Subscribe JMS Messaging Models

    Difference between Point-To-Point and Publish/Subscribe JMS Messaging Models   Point-to-Point (PTP) ...

  3. Dev GridControl,GridView 显示多行文本及合并相同单元格

    显示多行文本的方法 首先把gridcontrol的views的Optionsview里的RowAutoHeight设置为True 在In-place Editor Repository 里添加 Mem ...

  4. SVG 路径(path)

    本文转自:https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Paths <path>元素是SVG基本形状中最强大的一个,它 ...

  5. Android-------------获取手机IP地址

    ##帮助类PhoneNetStateUtil package com.funs.PhoneIPAddress.utils; /**      * 手机联网状态工具类 需要的权限 WIFI时:</ ...

  6. java基础2

    //第一个程序 用super访问父类中被隐藏的成员变量和被重写的方法 package foxe; class superClass{ int x; superClass(){ x=4; System. ...

  7. C++中的数组和指针

    #include <iostream> #include <set> using namespace std; int main() { ] = {,,,,,}; ]; p = ...

  8. javascript 过滤空格

    1: 过滤首尾空格trim.2:过滤左边空格ltrim    3:过滤右边空格 用正则方法写成三个函数. <script type="text/javascript"> ...

  9. NSString 去掉前后空格或回车符

    NSString *string = @" spaces in front and at the end "; NSString *trimmedString = [string ...

  10. 暴力破解UltraEdit v21 无需注册

    一.复制一份UltraEdit安装目录中的主程序uedit32.exe,到任意目录,用UltraEdit打开复制的uedit32.exe文件. 二.修改以下内容 原来:00094750h: BE DC ...