[Regular Expressions] Find Groups of Characters, and ?:
We'll capture groups of characters we wish to match, use quantifiers with those groups, and use references to those groups in String.prototype.replace.
Let's see we have set of similar string starting with 'foo'
var str = `
foobar
fooboo
foobaz
`;
And what we want to do is replace any 'foobar' & 'foobaz' with '**foobar**' && '**foobaz**' :
var str = `
foobar
fooboo
foobaz
`; var regex = /foo(bar|baz)/g; console.log(str.replace(regex, '**foo$1**')); /*
"
**foobar**
fooboo
**foobaz**
"
*/
$1, capture the gourp and save into memory.
Another example:
Let's say we what to get the area code for each number.
var str = `800-456-7890
(555) 456-7890
4564567890`;
So the result for the input should be '800, 555, 456'.
Todo this,
first: divide those number into xxx xxx xxxx, 3 3 4 group:
var regex = /\d{3}\d{3}\d{4}/g;
Second: now the only last one match, because, between group, there can be 'empty space' or '-':
Use:
\s // for space
- // for -
[\s-] // for select one element inside [], so \s or -
[\s-]? // 0 or more
SO:
var regex = /\d{3}[\s-]?\d{3}[\s-]?\d{4}/g;
Third: we need to match ():
\(? // match (: can be 0 or 1
\)? // match ) : can be 0 or 1
SO:
var regex = /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}/g;
Last: we need to capture the first 3 digital number group. use (xxx):
var regex = /\(?\(d{3})\)?[\s-]?\d{3}[\s-]?\d{4}/g;
then console.log the captured group:
console.log(str.replace(regex, 'area code: $1')) /* "area code: 800
area code: 555
area code: 456"
*/
Example 3:
re-format the number to xxx-xxx-xxxx:
var str = `800-456-7890
(555) 456-7890
4564567890`; var regex = /\(?(\d{3})\)?[\s-]?(\d{3})[\s-]?(\d{4})/g; var res = str.replace(regex, "$1-$2-$3"); console.log(res); /*
"800-456-7890
555-456-7890
456-456-7890"
*/
------------------
As we said, (xx) actually capture the group value and store into the memory, if you don't store tha reference into the memory, you can do:
(?:xxx) // ?: won't store the reference into the memory console.log(str.replace(regex, 'area code: $1')) /*
"area code: $1
area code: $1
area code: $1"
*/
-----------------------------
-----------------
(^\d{2}\/\d{2}\/(?:2015|2016) (\d{2}:\d{2}$))
------------------------------
/((?:sword|flat|blow)fish)/gim
--------------
Grab Your Passports
/\d{9}\d([A-Z]{3})(\d{6})\d([a-z])\d{23}/gim
[Regular Expressions] Find Groups of Characters, and ?:的更多相关文章
- PCRE Perl Compatible Regular Expressions Learning
catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...
- Regular Expressions --正则表达式官方教程
http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...
- Introducing Regular Expressions 学习笔记
Introducing Regular Expressions 读书笔记 工具: regexbuddy:http://download.csdn.net/tag/regexbuddy%E7%A0%B4 ...
- 正则表达式(Regular expressions)使用笔记
Regular expressions are a powerful language for matching text patterns. This page gives a basic intr ...
- Python re module (regular expressions)
regular expressions (RE) 简介 re模块是python中处理正在表达式的一个模块 r"""Support for regular expressi ...
- 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 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 ...
- [转]8 Regular Expressions You Should Know
Regular expressions are a language of their own. When you learn a new programming language, they're ...
- [Python] Regular Expressions
1. regular expression Regular expression is a special sequence of characters that helps you match or ...
随机推荐
- [Redux] Extracting Presentational Components -- TodoApp
Finally, I just noticed that the to-do app component doesn't actually have to be a class. I can turn ...
- Swift纯代码走进UICollectionView
Swift对于一门新的iOS编程语言,他的崛起是必然的 我们这群老程序员们学习新的技能也是必然的 不接受新技能将被这大群体无情的淘汰 So 我欣然接受这门看似不成熟的语言 下面我们说说Swift中比较 ...
- JSP实现分页功能
分页须知知识点: (1)JDBC2.0的可滚动结果集. (2)HTTP GET请求. 一.可滚动结果集 Connection con = DriverManager.getConnection( ...
- <经验杂谈>C#/.Net中xml的Serialization序列化与DeSerializetion反序列化
1.先讲概念:.Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象.Serializ ...
- C#的位运算符
C#的位运算符&,| ,^ ,<<,>>2008年08月01日 星期五 15:52位 运 算我们知道任何信息在计算机中都是以二进制的形式保存的位操作符就是对数据按二进制 ...
- AVT Vimba与OpenCV环境配置
近来,由于项目需求,需要使用AVT的一款相机采集图像并进行相应的算法处理.环境的配置过程较为复杂,特此记录,以做备忘.也给有需要的小伙伴们一些key point的分享. 搭建环境:Windows7 + ...
- Jboss基础及简单的应用
初学Jboss,对于Jboss的基础认识以及配置做一些记录 Jboss基础: JBoss是什么–基于J2EE的应用服务器–开放源代码–JBoss核心服务不包括支持servlet/JSP的WEB容器,一 ...
- akoj-1048-求某一整数序列的全排列问题
求某一整数序列的全排列问题 Time Limit:1000MS Memory Limit:65536K Total Submit:35 Accepted:16 Description 现有一整数序列 ...
- Xcode把应用程序打包成ipa
Xcode把应用程序打包成ipa 分类: App Store2012-11-20 15:47 11722人阅读 评论(0) 收藏 举报 Xcode教程 Xcode4发布测试 打包Archive操作是本 ...
- 做10年Windows程序员与做10年Linux程序员的区别(附无数评论)(开源软件相当于熟读唐诗三百首,不会作诗也会吟)
如果一个程序员从来没有在linux,unix下开发过程序,一直在windows下面开发程序, 同样是工作10年, 大部分情况下与在linux,unix下面开发10年的程序员水平会差别很大.我写这篇文章 ...