[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 ...
随机推荐
- mysql高可用方案MHA介绍
mysql高可用方案MHA介绍 概述 MHA是一位日本MySQL大牛用Perl写的一套MySQL故障切换方案,来保证数据库系统的高可用.在宕机的时间内(通常10-30秒内),完成故障切换,部署MHA, ...
- C# 导出Excel 多个Sheet
以下代码中最关键的代码是 Worksheet mSheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.Worksheets.Add(miss, ...
- .net 调用Oracle.Data.Access 组件提供的用于批量操作的方法—获取数据库表结构方法和跟参数赋值方法
1./// <summary> /// 获取当前目标表结构 /// </summary> /// <param name="tableName"> ...
- DTO学习系列之AutoMapper(五)----当EntityFramework爱上AutoMapper
有时候相识即是一种缘分,相爱也不需要太多的理由,一个眼神足矣,当EntityFramework遇上AutoMapper,就是如此,恋爱虽易,相处不易. 在DDD(领域驱动设计)中,使用AutoMapp ...
- hdu 5586 sum
Problem Description There is a number sequence A1,A2....An,you can select a interval [l,r] or not,al ...
- C++中cin输入类型不匹配解决方法
#include <iostream> #include <set> using namespace std; int main() { int a; cin>>a ...
- git命令简图
基本概念 版本库(repository)=.git目录 工作区(working)=当前工作的目录 暂存区(stage)=临时缓存 图示 仓库操作 分支操作 工作区操作 参考链接(相当好) http:/ ...
- C++第一课(2013.9.26 )
//C++三大特性:封装,继承,多态 //C++新增的数据类型:bool型 一个字节 真 true 假 false //case 定义变量的问题 ; switch(nValue) { : { prin ...
- 安装apache服务器时遇到只能本地访问,局域网内其他电脑不能访问apache:
安装apache服务器时遇到只能本地访问,局域网内其他电脑不能访问apache:1.查看selinux运行状态及关闭selinux/usr/sbin/sestatus -v文本模式关闭selinux: ...
- HOG detectMultiScale 参数分析
前段时间学习了HOG描述子及其与SVM结合在行人检测方面的应用. 当我们用训练好的模型去检测测试图像时,我们会用到detectMultiScale() 这个函数来对图像进行多尺度检测. 这是openc ...