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 ?:的更多相关文章

  1. PCRE Perl Compatible Regular Expressions Learning

    catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...

  2. Regular Expressions --正则表达式官方教程

    http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...

  3. Introducing Regular Expressions 学习笔记

    Introducing Regular Expressions 读书笔记 工具: regexbuddy:http://download.csdn.net/tag/regexbuddy%E7%A0%B4 ...

  4. 正则表达式(Regular expressions)使用笔记

    Regular expressions are a powerful language for matching text patterns. This page gives a basic intr ...

  5. Python re module (regular expressions)

    regular expressions (RE) 简介 re模块是python中处理正在表达式的一个模块 r"""Support for regular expressi ...

  6. 8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

  7. 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. [转]8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

  9. [Python] Regular Expressions

    1. regular expression Regular expression is a special sequence of characters that helps you match or ...

随机推荐

  1. Sybase自增字段跳号的解决方法

    Sybase自增字段跳号原因及影响: 在Sybase数据库中如果数据库在开启的情况下,因为非正常的原因(死机.断电)而导致数据库服务进程强制结束. 那么自动增长的字段将会产生跳号的情况,再往数据表里面 ...

  2. (转)@@trancount解析

    在处理事务的时候,一般都用RollBack Transaction来回滚,但是如果在嵌套事务中这样使用的话,就会出现错误. 在SqlServer里,嵌套事务的层次是由@@TranCount全局变量反映 ...

  3. 使用QTP打开应用程序的三种方法

    1. systemUtil.Run ‘SystemUtil对象的Run方法 SystemUtil.Run “http://192.168.11.82/XXX” 参数实例: File:“http://1 ...

  4. Silverlight Visifire控件 后台设置颜色

    ColorSet cs = new ColorSet(); cs.Id = "colorset1"; // 设置ColorSet 的 Id 为 colorset1 1.cs.Bru ...

  5. SQL中将某个表中的多行数据在一个字段显示

    项目需求:将某个表中的多行数据在一个字段显示,如下: 比如表A中有字段 ID,NAME, 表B中有字段ID,PID,DES, 表A,表B中的数据分别如下: ID NAME1 张三2 李四 ID PID ...

  6. ANSI与UINCODE编码

    简要说明: ANSI是一种字符代码,为使计算机支持更多语言,通常使用 0x80~0xFF 范围的 2 个字节来表示 1 个字符. Uincode(统一码.万国码.单一码)是计算机科学领域里的一项业界标 ...

  7. 费马小定理&欧拉定理

    在p是素数的情况下,对任意整数x都有xp≡x(mod p).这个定理被称作费马小定理其中如果x无法被p整除,我们有xp-1≡1(mod p).利用这条性质,在p是素数的情况下,就很容易求出一个数的逆元 ...

  8. UWP开发小记

    针对个人的上一篇文章中提到的遇到的几个问题,做一下个人解答 DLL部署的问题,可以将DLL添加到工程中,属性中设置content为true,这样,部署目录下就会有这个文件. 需要说明的是,这个文件确实 ...

  9. ssh-agent自启动加key脚本

    公司使用到阿里云. 需要使用 ssh-agent forward 来跳转.为了方便自己就写了这个脚本 1 #!/bin/sh 2 # auto start ssh-agent and add key ...

  10. 火狐的bug

    初次启动火狐的界面并且默认是最大化的情况下,第一个业签时会发现火狐的浏览器无法达到下边框,请看图 途中可以看到,body区域没有填充满浏览器可用区域.但是当浏览器已经启动页签,现在是第二个页签时,则不 ...