JavaScript / ECMAScript

Triple-Quoted String (Python / TS) in JS

Match triple-quoted strings (Python docstrings, TypeScript triple-quote, etc.) including newlines.

Try it in the JS tester →

Pattern

regexJS
"""([\s\S]*?)"""   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\"\"\"([\\s\\S]*?)\"\"\"", "g");
const input = "def foo():\\n    \"\"\"Docstring here.\"\"\"\\n    pass";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

The opening and closing """ bracket the string. ([\s\S]*?) is the lazy any-character-including-newline pattern: [\s\S] is the universal-character idiom (avoids needing the s/dotAll flag), and *? keeps the match minimal so adjacent triple-quote blocks don't merge.

Examples

Input

def foo():\n """Docstring here."""\n pass

Matches

  • """Docstring here."""

Input

a = """line1\nline2""" b = """third"""

Matches

  • """line1\nline2"""
  • """third"""

Input

no triple quotes

No match

Same pattern, other engines

← Back to Triple-Quoted String (Python / TS) overview (all engines)