[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 ...
随机推荐
- [Regular Expressions] Find Repeated Patterns
Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum an ...
- 使用Gradle构建Android应用内测版本
在开发应用的过程中,有时候需要比较当前线上版本和正在开发中的版本差异,目前的做法只能是在两个不同的设备上面安装线上版本和开发中的版本,因为当前版本在调试过程中会覆盖旧版本.本文通过使用gradle来构 ...
- textField:shouldChangeCharactersInRange:replacementString:
http://blog.csdn.net/mamong/article/details/44964801
- 刷新 tableview
UITableView对于iOS开发者来说一定不会陌生,很有可能你的APP很多界面都用到它.关于UITableView的文章,想必已经不计其数,没事可以多看看.特别是UITableView优化的文章, ...
- JVM的内存区域划分划分及作用
- OpenGL ES 2.0 限定符
限定符 说明 作用 attribute 一般用于各个顶点各不相同的量,如顶点位置.颜色等 属性限定符,修饰的变量用来接收渲染管线传递进顶点着色器的当前顶点的各种属性值. 只能用来修饰符点数标量,浮点数 ...
- linux 下配置mysql区分大小写(不区分可能出现找不到表的情况)怎么样使用yum来安装mysql
Linux 默认情况下,数据库是区分大小写的:因此,要将mysql设置成不区分大小写 在my.cof 设置 lower_case_table_names=1(1忽略大小写,0区分大小写) 检查方式:在 ...
- Avro基础
一.Avro的基本功能 1.定义了数据模式文件的语法,一般使用json文件.以及一些数据基本类型与复杂类型. 2.定义了数据序列化到文件后的数据格式,此格式可供各种语言进行读取. 3.为部分语言定义了 ...
- Web项目中用模板Jsp页面引入所有静态样式脚本文件(js,css等)
这样的好处是不需要再每个页面中都添加太多的外链接(不会减少请求数量),但对开发会更快捷,如果更改这些文件的位置或名称,只需要更改模板文件,不需要一个一个页面复制粘贴:同时可以为不同jsp页面组创建不同 ...
- document.cookie
概念相关: cookie是存于用户硬盘上的一个文件,对应一个域名,当浏览器再次访问这个域名时,便使用这个cookie. cookie 可以跨越一个域名下的多个网页,但不能跨越多个域名使用. cooki ...