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. Andriod Studio科学文章——4.常见问题解答有关编译

    1.android未安装支持库 只有编译,下面的例子演示了提样: Could not find any version that matches com.android.support:appcomp ...

  2. 【足迹C++primer】46、动态存储类

    动态存储类 StrVec Class Design StrVec Class Definition class StrVec { public: //构造函数 StrVec():elements(nu ...

  3. Android字数限制的EditText实现方案研究

    在应用开发中,有时需要实现有字数限制的EditText,首先来分析下市面上存在的类似实现方案吧,好有个感性的认识. [方案一:腾讯微博] 每个中文字符算一个字数,每两个英文字符算一个字数,当用户输入内 ...

  4. 一张图搞懂 Javascript 中的原型链、prototype、__proto__的关系 转载加自己的总结

    1. JavaScript内置对象 所谓的内置对象 指的是:JavaScript本身就自己有的对象 可以直接拿来就用.例如Array String 等等.JavaScript一共有12内置对象    ...

  5. jquery,js常用特效名称

  6. CSS background 属性 总结

    CSS background 属性总结

  7. java web实现读取指定盘符下的图像(二)

    之前写了一篇文章是关于如何读取指定盘符下的图片,虽然功能可以实现,但是使用的是I/O流的方式,效率不高.现在发现还有一个更好的办法,使用也更加的方便. 我们知道,当我们的图片是放在tomcat下web ...

  8. oracle登录错误(ORA-01033:ORACLE initialization or shutdown in progress

    在网上查找出现这种错误的原因一般是因为删除数据库 安装文件下的某个文件造 成的, 但是在我们的服务器上是因为 机子重启(意外断电)了一下就造 成这样的错误,不过解决办法是一样的,下面贴上解决方案 解决 ...

  9. 0301——SearchController

    创建显示的页面 SearchViewController * searchVC = [[SearchViewController alloc]init]; 告诉搜索控制器将结果显示在创建的页面上 se ...

  10. sqlserver2005重新安装(安装汇编错误,安装程序无法连接到数据库服务进行服务配置)

    2014-01-09 16:41 1687人阅读 评论(1) 收藏 举报 分类: 数据库(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. sqlserver2005重新安装(安装汇编错误, ...