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. 内存测试工具memtester

    Memtester是用户态工具,用于测试内存子系统的故障.非常方便,支持32位 或64位Unix-like系统.对于硬件开发开发者来说,memtester可以定位到物理地址. 1. 安装 下载地址ht ...

  2. Android实现左右滑动效果

    本示例演示在Android中实现图片左右滑动效果.   关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现.接下来 ...

  3. Tomcat6+nginx集群,达到负载均衡和session复制

    nginx+tomcat做web项目集群,达到负载均衡.故障转移.session复制功能. 1.nginx配置文件见上一篇“nginx配置文件(反向代理+集群+动静分离)” 2.tomcat集群,修改 ...

  4. table超过30个字段如何处理呢? bootstrap

    样式: @media (max-width: 768px) { .table-supplier { width: 100%; height: 100%; margin-bottom: 12.75px; ...

  5. 怎么判断PC端浏览器内核

    browser = {             /**              * @property {boolean} ie 检测当前浏览器是否为IE              */       ...

  6. poj3122 binary search 实数区间

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14536   Accepted: 4979   Special Ju ...

  7. (原)python中import caffe提示no module named google.protobuf.internal

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5993405.html 之前在一台台式机上在python中使用import caffe时,没有出错.但是 ...

  8. 如何在hadoop中控制map的个数

    hadooop提供了一个设置map个数的参数mapred.map.tasks,我们可以通过这个参数来控制map的个数.但是通过这种方式设置map的个数,并不是每次都有效的.原因是mapred.map. ...

  9. PC机安装android apk | adb install -r

    PC 下载 *****.apk 通过adb直接安装到android系统

  10. JQuery基础学习总结

    JQuery基础学习总结 简单总结下JQuery: 一:事件 1.change事件 <!DOCTYPE html> <html lang="en"> < ...