资讯专栏INFORMATION COLUMN

Javascript 正则使用第一篇

CoXie / 2662人阅读

摘要:参数说明此处赋值,为了更好的观察的值,位置运行结果运行结果运行结果第一次匹配第二次匹配第三次匹配第四次匹配第五次匹配第六次匹配运行结果运行结果如果使用了全局匹配则只显示匹配到的全部字符串。也没有属性运行结果运行结果

replace() 参数说明

@param match
The matched substring. (Corresponds to $&.)

@param p1

@param p2

@param p3
The nth parenthesized submatch string,
provided the first argument to replace was a RegExp object.
(Corresponds to $1, $2, etc. above.) For example,
if /(a+)(+)/, was given, p1 is the match for a+, and p2 for +.

@param offset
The offset of the matched substring within the total string being examined. (For example,
if the total string was "abcd",
and the matched substring was "bc",
then this argument will be 1.)

@param string
The total string being examined.

@returns {*|string}

function replacer(match, p1, p2, p3, offset, string) {     
    console.log("match: " + match);
    console.log("string: " + string);
    //p1 is nondigits, p2 digits, and p3 non-alphanumerics
    console.log("p1: " + p1);
    console.log("p2: " + p2);
    console.log("p3: " + p3);   
    //此处赋值,为了更好的观察p1,p3的值,位置
    p1 = 1;
    p3 = 3;    
   return [p1, p2, p3].join(" - ");
}
newString = "111abc12345#$*%".replace(/([^d]*)(d*)([^w]*)/, replacer);
console.log(newString);
运行结果:
 * match: 111
 * string: 111abc12345#$*%
 * p1:
 * p2: 111
 * p3:
 * 1 - 111 - 3abc12345#$*%
function replacer2(match, p1, p2, p3, offset, string) {
    console.log("match: " + match);
    console.log("string: " + string);
    // p1 is nondigits, p2 digits, and p3 non-alphanumerics
    console.log("p1: " + p1);
    console.log("p2: " + p2);
    console.log("p3: " + p3);
    return [p1, p2, p3].join(" - ");
}
newString2 = "abc12345xxx#$*%".replace(/([^d]*)(d*)([^w]*)/, replacer2);
console.log(newString2);
运行结果:
 * match: abc12345
 * string: abc12345xxx#$*%
 * p1: abc
 * p2: 12345
 * p3:
 * abc - 12345 - xxx#$*%
function replacer3(match, p1, p2, p3, offset, string) {
    console.log("match: " + match);
    console.log("string: " + string);
    // p1 is nondigits, p2 digits, and p3 non-alphanumerics
    console.log("p1: " + p1);
    console.log("p2: " + p2);
    console.log("p3: " + p3);
    return [p1, p2, p3].join(" - ");
}
newString3 = "1111abc12345xxx#$*%2392039abc12345xxx#$*%".replace(/([^d]*)(d*)([^w]*)/g, replacer3);
console.log(newString3);
运行结果:
 * 第一次匹配

 * match: 1111 
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1:
 * p2: 1111
 * p3:

 * 第二次匹配

 * match: abc12345
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1: abc
 * p2: 12345
 * p3:


 * 第三次匹配

 * match: xxx#$*%2392039
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1: xxx#$*%
 * p2: 2392039
 * p3:


 * 第四次匹配

 * match: abc12345
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1: abc
 * p2: 12345
 * p3:

 * 第五次匹配

 * match: xxx#$*%
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1: xxx#$*%
 * p2:
 * p3:

 * 第六次匹配

 * match:
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1:
 * p2:
 * p3:
 * - 1111 - abc - 12345 - xxx#$*% - 2392039 - abc - 12345 -     xxx#$*% -  -  -  -
match()
function match1() {
    var str = "For more information, see Chapter 3.4.5.1",
        re = /(chapter d+(.d)*)/i,
        found = str.match(re);

    console.dir(found);
}

match1();
运行结果:
 * Array[3]
 * 0: "Chapter 3.4.5.1"
 * 1: "Chapter 3.4.5.1"
 * 2: ".1"
 * index: 26
 * input: "For more information, see Chapter 3.4.5.1"
 * length: 3
 *
 * [input]:
 *         which contains the original string that was parsed
 * [0]:
 *     the first match
 * [1]:
 *     the first value remembered from (Chapter d+(.d)*).
 * [2]:
 *     ".1" is the last value remembered from (.d).
 * ---------------------------------------------------
 */
function match2() {
    var str = "For more information, see Chapter 3.4.5.1",
        re = /(chapter d+(.d)*)/gi,
        found = str.match(re);

    console.dir(found);
}

match2();
运行结果:
 * Array[1]
 * 0: "Chapter 3.4.5.1"
 * length: 1
 *
 * 如果使用了全局匹配g, 则只显示匹配到的全部字符串。
 * 也没有input属性
function match3() {
    var str = "qbylucky@gmail.com",
        re = /(.*)@(.*).(.*)/,
        found = str.match(re);

    console.dir(found);
}

match3();
运行结果:
 * Array[4]
 * 0: "qbylucky@gmail.com"
 * 1: "qbylucky"
 * 2: "gmail"
 * 3: "com"
 * index: 0
 * input: "qbylucky@gmail.com"
 * length: 4
function match4() {
    var str = "qbylucky@gmail.com",
        re = /(.*)@(.*).(.*)/g,
        found = str.match(re);

    console.dir(found);
}

match4();
运行结果:
 * Array[1]
 * 0: "qbylucky@gmail.com"
 * length: 1

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/78085.html

相关文章

  • JavaScript正则表达式学习笔记(二) - 打怪升级

    摘要:本文接上篇,基础部分相对薄弱的同学请移步正则表达式学习笔记一理论基础。正则表达式标志符全局匹配,即找到所有匹配的。方法返回结果的格式不一致问题这个问题上文正则表达式学习笔记一理论基础也有体现,这里再单独拿来说一说,以加深记忆。 showImg(https://segmentfault.com/img/remote/1460000014261596?w=600&h=338); 本文接上篇...

    Jioby 评论0 收藏0
  • 正则表达式之初入江湖

    摘要:拿举例子只想说明你总会在一些阴暗的角落遇到正则表达式,为了到时候不至于一头雾水,我们最好简单的了解一下正则表达式的使用。 为什么要学正则表达式 很多人对正则表达式的认知只是在进行表单验证的时候在网上搜一段正则表达式进行copy,实际工作上好像很难遇到大段的正则表达式 我第一次看到大量的正则使用是在jQuery源码中,当时看的头疼只好草草的看下大概思路不了了之,但是到今天我依然不认为这种...

    caige 评论0 收藏0
  • task0002(一)- JavaScript数据类型及语言基础

    摘要:不过让流行起来的原因应该是是目前所有主流浏览器上唯一支持的脚本语言。经过测试,数字字符串布尔日期可以直接赋值,修改不会产生影响。再考虑对象类型为或者的情况。对于结果声明其类型。判断对象的类型是还是,结果类型更改。 转载自我的个人博客 欢迎大家批评指正 1. 第一个页面交互 这里最需要学习的老师的代码中,每一部分功能都由函数控制,没有创建一个全部变量。且最后有一个函数来控制执行代码...

    elarity 评论0 收藏0
  • 正则表达式

    摘要:本文内容共正则表达式火拼系列正则表达式回溯法原理学习正则表达式,是需要懂点儿匹配原理的。正则表达式迷你书问世了让帮你生成和解析参数字符串最全正则表达式总结验证号手机号中文邮编身份证地址等是正则表达式的缩写,作用是对字符串执行模式匹配。 JS 的正则表达式 正则表达式 一种几乎可以在所有的程序设计语言里和所有的计算机平台上使用的文字处理工具。它可以用来查找特定的信息(搜索),也可以用来查...

    bang590 评论0 收藏0
  • JavasScript重难点知识

    摘要:忍者级别的函数操作对于什么是匿名函数,这里就不做过多介绍了。我们需要知道的是,对于而言,匿名函数是一个很重要且具有逻辑性的特性。通常,匿名函数的使用情况是创建一个供以后使用的函数。 JS 中的递归 递归, 递归基础, 斐波那契数列, 使用递归方式深拷贝, 自定义事件添加 这一次,彻底弄懂 JavaScript 执行机制 本文的目的就是要保证你彻底弄懂javascript的执行机制,如果...

    forsigner 评论0 收藏0

发表评论

0条评论

CoXie

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<