ES6中正则的扩展加入了后行断言的方案

本文介绍了JavaScript中的正则扩展——后行断言(Lookbehind Assertions),包括正向和负向后行断言的用法,以及它们在匹配过程中的工作原理。文中还探讨了在实现后行断言时可能遇到的问题,如组匹配和反斜杠引用的行为变化。
摘要由CSDN通过智能技术生成

javascript中语言的正则表达式只支持先行断言(lookhead)和先行否定断言。不支持后行断言和后行断言,目前,有一个提案,这个提案是github一位叫做 Gorkem Yakin, Nozomu Katō的大神提出的,并被ES组织录入标准!

以下是原文:

RegExp Lookbehind Assertions

Authors: Gorkem Yakin, Nozomu Katō

Introduction

Lookarounds are zero-width assertions that match a string without consuming anything. ECMAScript has lookahead assertions that does this in forward direction, but the language is missing a way to do this backward which the lookbehind assertions provide. With lookbehind assertions, one can make sure that a pattern is or isn't preceded by another, e.g. matching a dollar amount without capturing the dollar sign.

High Level API

There are two versions of lookbehind assertions: positive and negative.

Positive lookbehind assertions are denoted as (?<=...) and they ensure that the pattern contained within precedes the pattern following the assertion. For example, if one wants to match a dollar amount without capturing the dollar sign, /(?<=\$)\d+(\.\d*)?/ can be used, matching '$10.53' and returning '10.53'. This, however, wouldn't match €10.53.

Negative lookbehind assertions are denoted as (?<!...) and, on the other hand, make sure that the pattern within doesn't precede the pattern following the assertion. For example, /(?<!\$)\d+(?:\.\d*)/ wouldn't match '$10.53', but would '€10.53'.

All regular expression patterns, even unbounded ones, are allowed as part of lookbehind assertions. Therefore

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值