[Regular Expressions] Match the Same String Twice
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的更多相关文章
- [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: ...
- PCRE Perl Compatible Regular Expressions Learning
catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...
- 8 Regular Expressions You Should Know
Regular expressions are a language of their own. When you learn a new programming language, they're ...
- Regular Expressions --正则表达式官方教程
http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...
- 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 ...
- Introducing Regular Expressions 学习笔记
Introducing Regular Expressions 读书笔记 工具: regexbuddy:http://download.csdn.net/tag/regexbuddy%E7%A0%B4 ...
- [转]8 Regular Expressions You Should Know
Regular expressions are a language of their own. When you learn a new programming language, they're ...
- 正则表达式(Regular expressions)使用笔记
Regular expressions are a powerful language for matching text patterns. This page gives a basic intr ...
- [Python] Regular Expressions
1. regular expression Regular expression is a special sequence of characters that helps you match or ...
随机推荐
- linux高级技巧:heartbeat+lvs(一)
1.heartbeat一个简短的引论: Heartbeat 项目是 Linux-HA project的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组 ...
- _js day10
- DEV PivotGridControl 全选行或列
foreach (string item in fieldProductName.FilterValues.Values) { pivotGridControl.Cells.SetSelectionB ...
- C#上传图片同时生成缩略图,控制图片上传大小。
#region 上传图片生成缩略图 /// <summary> /// 上传图片 /// </summary> /// <param name="sender& ...
- 关于sql2005 与 myeclipse进行连接出现的小问题
C盘目录下没有jdbc这个文件夹,所以从网上下一个 这个是2008连接jdbc用的 正常解压第一个到相应的目录 主要是注意一个叫tcp/ip的协议,米有找到64位的 点击这里的tcp ip就哦了但是他 ...
- linux下查找文件和文件内容
find /xxx -name "*" | xargs grep "某内容" /xxx表示路径,"*"表示在含有某关键字名字下的文件中查找, ...
- -webkit-appearance: none;
今天在web群里聊天的时候,发现了这个东东,我好像不怎么认识他,于是查了下关于他的信息: 抄的例子, ----------- IOS环境下的按钮都是经过美化的,但通常我们在设计web app的时候不需 ...
- Linux免SSH密码登录
SSH免密码登录,做个总结吧! 1.安装SSH服务(略过) 2.场景:需要配置主机A无密码登录主机B 在主机A上执行如下: cd ~/.ssh ssh-keygen -t rsa 生成密钥文件 cp ...
- Oracle字符函数(转换大小写,替换等)
在oracle中,有一些字符函数: upper(字符串):转换为大写lower(字符串):转换为小写initcap(字符串):首字母大写replace(字符串1,字符串2,字符串3):将串1中所有的串 ...
- 记事本创建servlet在tomcat中发布基本思路
在webapps中新建文件夹H,在其中再创建WEB-INF文件夹,在创建classes文件夹和web.xml文件,web.xml需要配置一下,classes文件夹中存放Servlet经编译过的clas ...