阿里云主机折上折
  • 微信号
Current Site:Index > Regular expression lookbehind assertion

Regular expression lookbehind assertion

Author:Chuan Chen 阅读数:4589人阅读 分类: JavaScript

ECMAScript 9 introduced lookbehind assertions in regular expressions, addressing the previous limitation of only supporting lookahead assertions. Lookbehind assertions are divided into positive lookbehind and negative lookbehind, which are used to match strings that conform to or do not conform to a specific pattern without consuming characters.

Basic Concepts of Lookbehind Assertions

Lookbehind assertions allow checking whether the content preceding the current position matches a specific pattern when matching strings. Positive lookbehind uses the syntax (?<=...), indicating that the position must be preceded by the pattern .... Negative lookbehind uses the syntax (?<!...), indicating that the position must not be preceded by the pattern ....

// Positive lookbehind example
const positiveLookbehind = /(?<=\$)\d+/;
console.log(positiveLookbehind.exec('$100')); // ["100", index: 1, ...]

// Negative lookbehind example
const negativeLookbehind = /(?<!\$)\d+/;
console.log(negativeLookbehind.exec('€100')); // ["100", index: 1, ...]

Usage of Positive Lookbehind

Positive lookbehind is used to match strings that must be preceded by a specific pattern. For example, matching numbers following a currency symbol:

const pricePattern = /(?<=\$|€|¥)\d+(\.\d{2})?/g;
const text = 'Prices: $100, €200, ¥300.50';
console.log(text.match(pricePattern)); // ["100", "200", "300.50"]

Another example is matching content following a specific word:

const afterHello = /(?<=Hello, )\w+/;
console.log(afterHello.exec('Hello, world')); // ["world", index: 7, ...]

Usage of Negative Lookbehind

Negative lookbehind is used to match strings that must not be preceded by a specific pattern. For example, matching numbers not preceded by a currency symbol:

const noCurrencyPattern = /(?<!\$|€|¥)\b\d+\b/g;
const text = '100 $200 300 €400 500';
console.log(text.match(noCurrencyPattern)); // ["100", "300", "500"]

Another example is matching words not enclosed in quotes:

const notInQuotes = /(?<!["'])\b\w+\b(?!["'])/g;
const text = 'word "quoted" not_quoted';
console.log(text.match(notInQuotes)); // ["word", "not_quoted"]

Combining Lookbehind Assertions with Capturing Groups

Lookbehind assertions can be combined with capturing groups to achieve more complex matching logic. For example, extracting numbers following a specific prefix:

const prefixNumber = /(?<=ID: )(\d+)/;
const text = 'ID: 12345';
const match = prefixNumber.exec(text);
console.log(match[1]); // "12345"

Another example is matching specific words not within HTML tags:

const notInTag = /(?<!<[^>]*)\bimportant\b(?!>[^<]*>)/g;
const html = '<div>important</div> not important';
console.log(html.match(notInTag)); // ["important"] (only matches the second one)

Performance Considerations for Lookbehind Assertions

Lookbehind assertions may impact the performance of regular expressions in certain scenarios, especially when processing long strings, as the engine needs to backtrack and check preceding content. For example:

// Potentially slow lookbehind
const slowPattern = /(?<=\b\w{10,})\d+/;
const longText = 'abcdefghij1234567890';
console.log(slowPattern.test(longText)); // true

Browser Compatibility and Transpilation

Lookbehind assertions in ECMAScript 9 are supported in modern browsers but require transpilation for older environments. Tools like Babel can ensure compatibility:

// Babel configuration example
{
  "plugins": ["@babel/plugin-transform-named-capturing-groups-regex"]
}

Practical Use Cases

Lookbehind assertions are useful in form validation, log analysis, and text processing. For example, validating that a password does not contain the username:

const username = 'admin';
const passwordPattern = new RegExp(`(?<!${username}).{8,}`);
console.log(passwordPattern.test('secure123')); // true
console.log(passwordPattern.test('admin123')); // false

Another example is extracting error messages following a specific time from logs:

const errorAfterTime = /(?<=15:30:00\s).*ERROR.*/g;
const logs = `
15:29:59 INFO System started
15:30:01 ERROR Disk full
15:30:02 WARNING Memory low
`;
console.log(logs.match(errorAfterTime)); // ["ERROR Disk full"]

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.