博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Regular Expressions] Find Groups of Characters, and ?:
阅读量:4562 次
发布时间:2019-06-08

本文共 2322 字,大约阅读时间需要 7 分钟。

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-78904564567890`;

 

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: 800area code: 555area code: 456"*/

 

Example 3:

re-format the number to xxx-xxx-xxxx:

var str = `800-456-7890(555) 456-78904564567890`;var regex = /\(?(\d{3})\)?[\s-]?(\d{3})[\s-]?(\d{4})/g;    var res = str.replace(regex, "$1-$2-$3");console.log(res);/*"800-456-7890555-456-7890456-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 memoryconsole.log(str.replace(regex, 'area code: $1'))/*"area code: $1area code: $1area 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

转载于:https://www.cnblogs.com/Answer1215/p/5189836.html

你可能感兴趣的文章
RabbitMQ入门
查看>>
博弈入门
查看>>
spark计算平均值
查看>>
Thread
查看>>
Chrome浏览器安装插件时一直停留在"正在检查"的可用解决方法
查看>>
XPath
查看>>
【问底】徐汉彬:亿级Web系统搭建——单机到分布式集群(三)
查看>>
关于struts2的Unable to load configuration. - Class: java.net.Plain
查看>>
java分布式锁的处理
查看>>
350. Intersection of Two Arrays II java solutions
查看>>
[bzoj1485] [HNOI2009]有趣的数列
查看>>
【#】Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释...
查看>>
本地文本数据查询
查看>>
多语言的2种实现方式对比
查看>>
BZOJ4810 Ynoi2017由乃的玉米田(莫队+bitset)
查看>>
Java学习(十)
查看>>
[转]这才是真正的3D显示!Leap Motion推出次毫米级3D手动控制技术,让人手和影像融为一体...
查看>>
10月27日体会目标
查看>>
c# 前台js 调用后台代码
查看>>
2017-02-20 可编辑div中如何在光标位置添加内容
查看>>