Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Proposal to add the `s` (`dotAll`) flag to regular expressions in ECMAScript.

NotificationsYou must be signed in to change notification settings

tc39/proposal-regexp-dotall-flag

Repository files navigation

Status

This proposal is at stage 4 ofthe TC39 process.

Motivation

In regular expression patterns, the dot. matches a single character, regardless of what character it is. In ECMAScript, there are two exceptions to this:

ECMAScript recognizes the following line terminator characters:

  • U+000A LINE FEED (LF) (\n)
  • U+000D CARRIAGE RETURN (CR) (\r)
  • U+2028 LINE SEPARATOR
  • U+2029 PARAGRAPH SEPARATOR

However, there are more characters that, depending on the use case,could be considered as newline characters:

  • U+000B VERTICAL TAB (\v)
  • U+000C FORM FEED (\f)
  • U+0085 NEXT LINE

This makes the current behavior of. problematic:

  • By design, it excludessome newline characters, but not all of them, which often does not match the developer’s use case.
  • It’s commonly used to matchany character, which it doesn’t do.

The proposal you’re looking at right now addresses the latter issue.

Developers wishing to truly matchany character, including these line terminator characters, cannot use.:

/foo.bar/.test('foo\nbar');// → false

Instead, developers have to resort to cryptic workarounds like[\s\S] or[^]:

/foo[^]bar/.test('foo\nbar');// → true

Since the need to match any character is quite common, other regular expression engines support a mode in which. matches any character, including line terminators.

  • Engines that support constants to enable regular expression flags implementDOTALL orSINGLELINE/s modifiers.
    • Java supportsPattern.DOTALL.
    • C# and VB supportRegexOptions.Singleline.
    • Python supports bothre.DOTALL andre.S.
  • Engines that support embedded flag expressions implement(?s).
  • Engines that support regular expression flags implement the flags.

Note the established tradition of naming these modifierss (short forsingleline) anddotAll.

One exception is Ruby, wherethem flag (Regexp::MULTILINE) also enablesdotAll mode. Unfortunately, we cannot do the same thing for them flag in JavaScript without breaking backwards compatibility.

Proposed solution

We propose the addition of a news flag for ECMAScript regular expressions that makes. match any character, including line terminators.

/foo.bar/s.test('foo\nbar');// → true

High-level API

constre=/foo.bar/s;// Or, `const re = new RegExp('foo.bar', 's');`.re.test('foo\nbar');// → truere.dotAll// → truere.flags// → 's'

FAQ

What about backwards compatibility?

The meaning of existing regular expression patterns isn’t affected by this proposal since the news flag is required to opt-in to the new behavior.

How doesdotAll mode affectmultiline mode?

This question might come up since thes flag stands forsingleline, which seems to contradictm /multiline — except it doesn’t. This is a bit unfortunate, but we’re just following the established naming tradition in other regular expression engines. Picking any other flag name would only cause more confusion. The accessor namedotAll gives a much better description of the flag’s effect. For this reason, we recommend referring to this mode asdotAll mode rather thansingleline mode.

Both modes are independent and can be combined.multiline mode only affects anchors, anddotAll mode only affects..

When both thes (dotAll) andm (multiline) flags are set,. matches any character while still allowing^ and$ to match, respectively, just after and just before line terminators within the string.

Specification

Implementations

About

Proposal to add the `s` (`dotAll`) flag to regular expressions in ECMAScript.

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors5

Languages


[8]ページ先頭

©2009-2025 Movatter.jp