Home
Web Front-end
JS Tutorial
Grouping concepts and usage examples of javascript regular expressions



Grouping concepts and usage examples of javascript regular expressions
本文实例讲述了javascript正则表达式之分组概念与用法。分享给大家供大家参考,具体如下:
function matchDemo(){ var s; //该表达式分了三个组:d(b+)(d)、(b+)、(d)这个三个组(实际上是四个组,包括本身所有的表达式) //从最左边数第一个括号为第一个组,第二个括号为第二组,以此类推,分别对应的值为RegExp.$1和RegExp.$2的值 var re = new RegExp("(d(b+)(d))","ig"); var str = "cdbBdbsbdbdz"; //exec()返回的值是符合表达式查找出来的一个数组 var arr = re.exec(str); //符合d(b+)(d)这个表达式返回的值 s = "$1 contains: " + RegExp.$1 + ", RegExp.$1 : " + RegExp.$1.length + "\n"; //符合(b+)这个表达式返回的值 s += "$2 contains: " + RegExp.$2 + ", RegExp.$2 : " + RegExp.$2 + "\n"; //符合(d)这个表达式返回的值 s += "$3 contains: " + RegExp.$3 + ", RegExp.$3 : " + RegExp.$3; //得到匹配字符串最后一个字符在字符串中的位置,也就是下次匹配开始位置 alert(RegExp.lastIndex); //如果存在值,则返回的是最后一个分组的结果(返回任何正则表达式查找过程中最后括的子匹配) alert(RegExp.lastParen); //得到最后匹配的字符串(返回任何正则表达式搜索过程中的最后匹配的字符) alert(RegExp.lastMatch); //leftContext + lastMatch + rightContext == context alert(RegExp.leftContext); alert(RegExp.rightContext); //得到的结果为第二个分组的结果 alert(RegExp.$2); return(s); } alert(matchDemo());
Copy after login
function matchDemo2(){ var s,temp; //该结果分为两个组(b+)、(d),当然还包括全部(d(b+)(d))为默认的组 var re = new RegExp("d(b+)(d)","ig"); var str = "cdbBdbsbdbdz"; //arr的结果包括三个组(包括全部的模式)返回的结果 //dbBd、bB、d和dbd、b、d这两个模式匹配,而第二个和第三个数组实际上就是在第一个的基础上匹配(b+)、(d)这两个模式 //也就是说分组是在全匹配的模式下再进行匹配,起到"筛选"的功能 while((arr = re.exec(str)) != null) { alert(arr); temp = "$1 contains: " + RegExp.$1 + ", RegExp.$1.length : " + RegExp.$1.length + ",RegExp.$1.lastIndex:" + RegExp.$1.lastIndex; alert(temp); //$2属性表示的是匹配第二个分组,即(d)这个模式 s = "$2 contains: " + RegExp.$2 + ", RegExp.$2.length : " + RegExp.$2.length; alert(s); } } matchDemo2();
Copy after login
希望本文所述对大家JavaScript程序设计有所帮助。
更多javascript正则表达式之分组概念与用法实例相关文章请关注PHP中文网!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

HTTP Debugging with Node and http-console

Custom Google Search API Setup Tutorial
