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:

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

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

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

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

Code:

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

And of course, we can do better:

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

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

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

  1. 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.

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

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

  1. var regex = /<(\w+)>(.*)<\/\1>/g;
  1. var str = `<b>Bold</b><i>italics</i>`;
  2. var regex = /<(\w+)>(.*)<\/\1>/g;
  3.  
  4. console.log(str.replace(regex, '$2\n'));
  5.  
  6. /*
  7. "Bold
  8. italics
  9. "
  10. */

[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. IP转发和子网路由

    IP地址的分类 在TCP/IP协议中,协议栈分为4层.从上到下依次是应用层.运输层.网络层.网络接口层. IP协议就工作在网络层.IP协议将纷繁复杂的物理层协议屏蔽掉,对上层提供统一的描述和管理服务. ...

  2. python - 面向对象(一)

    python是一门面向对象的编程语言,python中的一切均是对象. 有对象就提到类,对象和类就像是儿子和老子的关系,是不可分的一对. 什么是类     类就是具有一些共同特性的事物的统称.好比人类, ...

  3. dataset导出成excel

    之前网上查找了很多关于这类的代码.要不是中文乱码,要不是就是太复杂.这个是我用过最好用的. //ds为数据源,filename为保存的文件名 publicvoidCreateExcel(DataSet ...

  4. 网站全局js代码

    这几天开始看公司的一套系统,整理的网站全局js代码 /*文件名:base.js功能说明:全站通用的全局变量及公用方法创建日期:2010-09-26*///引入jquery库文件document.wri ...

  5. Oracle decode函数 除数为零

    decode (expression, search_1, result_1)如果 expression结果=search_1结果,则返回result_1,类似 if elsedecode (expr ...

  6. C# 几十万级数据导出Excel,及Excel各种操作

    先上导出代码 /// <summary> /// 导出速度最快 /// </summary> /// <param name="list">&l ...

  7. 关于(x&y)+((x^y)>>1)的探究

    今天在程序员面试宝典上看到 int f(int x int y ) { return (x&y)+((x^y)>>1) } f(729,271) 结果为500 从式子中可以看出分为 ...

  8. keil中查看内存数据

    1.工具栏中 view->Memory Windows 然后  c:0 表示读取0地址开始的代码区数据  d:0 表示读取0地址开始的数据区数据  x:0表示读取0地址开始的外部数据区

  9. VC++下使用SQLite数据库

    老师最近给的上机题目有点变态,特别是写到最后,是需要写学生管理系统.如果C语言结合文件来操作的话,估计会比较麻烦(对文件里字符串的增删改查我都没有什么好点的算法).那就用数据库吧,我很自然的想到. 前 ...

  10. 修补--Redis未授权访问漏洞

    --------------------------------阿里云解决方案----------------------------------- 一.漏洞描述 Redis因配置不当可以导致未授权访 ...