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. iptables 实现centos内网机器访问外网

    环境:一台带外网和内网的机器,另一台只有内网,默认不能上网.两台机器都是centos系统带外网机器的外网ip为 123.221.20.11, 内网ip为 192.168.15.100内网机器的内网ip ...

  2. python-线程、进程、协程

    进程 && 线程 进程:是内存中的一个独立的句柄,我们可以理解为一个应用程序在内存中就是一个进程. 各个进程之间是内存相互独立,不可共享的 线程:每个应用运行之后就会对应启动一个主线程 ...

  3. Easyui 排序时 自动向后排传sort order 你妹真坑爹

    request 的时候 发现 sort 竟然是个数组!

  4. NSString 使用小结

    http://blog.csdn.net/ms2146/article/details/8654148

  5. oc随笔六:字典

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  6. WCF入门教程系列四

    一.概述 配置也是WCF编程中的主要组成部分.在 以往的.net应用程序中,我们会把DBConn和一些动态加载类及变量写在配置文件里.但WCF有所不同.他指定向客户端公开的服务,包括服务的地址. 服务 ...

  7. asp.net mvc4 远程验证

    [HttpGet] public ActionResult CheckToolsIdExists(string ToolsID) { using (BaseContext context = new ...

  8. angular.js学习

    1.第一个小例子 <!DOCTYPE html> <html> <body> <div ng-app=""> <p>在输 ...

  9. maven项目依赖被改为文件夹时如何改回lib

    如图

  10. jquery中checkbox全选失效的解决方法

    这篇文章主要介绍了jquery中checkbox全选失效的解决方法,需要的朋友可以参考下     如果你使用jQuery 1.6 ,代码if ( $(elem).attr(“checked”) ),将 ...