[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 ...
随机推荐
- UVALive 4043 Ants
KM 构图求最小权值匹配 保证最小的权值,所连的边一定是能够不相交的. Ants Time Limit: 3000MS Memory Limit: Unknown 64bit IO For ...
- mysql判断某个字符串是否包含另一个
like SELECT * FROM wx_webauth_userinfo where city LIKE "%台%";"; 结果: 函数find_in_set(str ...
- css3标签
-moz代表firefox浏览器私有属性 -ms代表ie浏览器私有属性 -webkit代表chrome.safari私有属性 -o代表opera私有属性 border-radius:2em; 向div ...
- asp.net实现将网页存为mht格式文件,类似于网页另存为mht功能
MHT 首先说一下什么是mht格式文件,MHT叫“web单一文件”,就是把网页中包含的图片,CSS文件以及HTML文件全部放到一个MHT文件里面,而且浏览器可以直接读取显示.可以通过ie浏览器将一个网 ...
- 自定义VS的ItemTemplates 实现任意文件结构
上一篇说到重写IHttpHandler实现前后端分离,这次说一下如何建立一个如下文件结构. VS建立webform时是根据模板来的.C#的模板目录如下: F:\Program Files (x86)\ ...
- js字母大小写转换
function a(){ document.getElementById("test").value = document.getElementById("test&q ...
- angularjs字符串插值($interpolate)
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...
- poj2251 三维简单BFS
D - (热身)简单宽搜回顾 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- 把python项目部署到centos里
.安装centos VMware9下面安装centos .在centos下面设置共享文件夹为你本地的论坛的代码,然后设置网络为桥接:直接连接到物理网络,赋值网络连接状态 .进入forum_svr.py ...
- 01--从根源种子CCNode说起
CCNode作为渲染框架的基类(暂且这样理解,CCObject为引擎基类)其中定义了绘制游戏元素相关的属性以及相关方法.属性当中需要注意的一个是Z坐标,在渲染框架中用来表示元素的遮挡关系,其值越小越容 ...