[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 ...
随机推荐
- bat处理文件
bat处理文件 作用:可以一次性执行多个命令的文件. 为什么要学bat处理文件? 快速运行一个软件一般都会把软件打包一个jar包,但是jar双击可以运行仅对图形化界面的软件起作用,对于控制台的程序是不 ...
- javascript 打开新窗口(window.open)
打开新窗口(window.open) open() 方法可以查找一个已经存在或者新建的浏览器窗口. 语法: window.open([URL], [窗口名称], [参数字符串]) 参数说明: URL: ...
- SuperSocket学习笔记(一)
这是根据我自己学习的经历整理出来的,如有不对之处,还请多多指教! SuperSocket源码下载 SuperSocket文档 安装并启动Telnet 学习方法: QuickStrart + 文档 参考 ...
- ORacle 复制表
create table r_register_company as select companyid,companyname,from grdata.r_register_company inser ...
- SQLSERVER 跨服 跨库
一.同服务器 select * from 数据库名.dbo.表名 select * from 数据库名..表名 dbo可以省略,点不能. 二.不同服务器 --新增服务器连接exec sp_addlin ...
- 选中CheckBoxList的值放到TextBox中,再次选中从textBox中删除
当选中checkboxlist中的值,直接放到文本框中,在checkboxlist的SelectedIndexChanged事件下执行方法, //将选中的值放到文本框中 ...
- URI和URL
URI(uniform resource identifier),统一资源标识符,用来唯一的标识一个资源. URL(uniform resource locator),统一资源定位器,它是一种具体的U ...
- struts2 result的type属性
目前只使用过以下3种,都是直接跳转到另一个action chain: 写法:<result name="success" type="chain"> ...
- mysql 常用语法
--创建数据库 CREATE DATABASE DB_NAME; --选中数据库 USE DB_NAME; --列出数据库列表 SHOW DATABASES; --删除数据库 DROP DATABAS ...
- pygame实现的黑白块游戏
运行时需要pygame库. 下载地址:http://files.cnblogs.com/files/zzrom/white.zip 程序截图: