[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 ...
随机推荐
- hdu-1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10219 Accepted: 4977 Des ...
- Java 编程下使用 Class.forName() 加载类
在一些应用中,无法事先知道使用者将加载什么类,而必须让使用者指定类名称以加载类,可以使用 Class 的静态 forName() 方法实现动态加载类.下面的范例让你可以指定类名称来获得类的相关信息. ...
- Gson源码分析之Json结构抽象和注解使用
github上的博客地址: http://chuyun923.github.io/blog/2015/01/06/gsonyuan-ma-fen-xi/ XML和Json作为最常用的两种网络传输格式而 ...
- 卸载rpm包提示:error: specifies multiple packages
–allmatches Remove all versions of the package which match PACKAGE_NAME. Normally an error is issue ...
- Android中多表的SQLite数据库(译)
原文: Android SQLite Database with Multiple Tables 在上一篇教程Android SQLite Database Tutorial中,解释了如何在你的And ...
- mac中遇到的mysql编码问题
由于项目有需要支持表情包输入数据库,自己做了一下技术测试,修改了my.cnf的权限为777.结果就操蛋了.编码错误.... 直到我无意地输入mysql -h,提示我/etc/my.cnf被忽略的一段话 ...
- 对话框(alert,prompt,confirm,showModalDialog)
alert大部分浏览器中会产生阻塞,confirm,prompt都会产生阻塞 关于showModalDialog的介绍:(转自JS中showModalDialog 详细使用) 基本介绍: ...
- ext4 grid edit 添加删除行
extjs--grid动态添加一行和删除一行: (1)选择rowEditing时,添加一行后的编辑方式为----startEdit(record,columnHeader) (2)选择cellEdit ...
- ECSHOP商城全站自定义TITLE标题设置
对于SEO来说,能让标题自定义的将会大大增加SEO效果,提高独立商城的流量,今天小编就收集从网上弄来ecshop全站自定义代码,很全哦! 1.Ecshop商品分类页如何实现自定义Title 最近发现很 ...
- Java学习笔记--Swing用户界面组件
很多与AWT类似. 事件处理参考:Java学习笔记--AWT事件处理 1.设计模式: 模型:存储内容视图:显示内容控制器:处理用户输入· 2. 文本输入常用组件 2.1 文本域: JLabel lab ...