How does any symbol appear in RegExp?



  • I need to get a line of data between ```:

    As an example from the line

    ```
    Я пожилой
    Дракон
    ```
    

    I need to get "Я пожилой\nДракон"
    I'll use RegExp for this. It's like they're marking absolutely any symbol, even with \n?



  • If you use regular expression in an environment that supports ECMAScript 2018+♪ You can use ♪ . to search any symbol together with https://262.ecma-international.org/12.0/#sec-get-regexp.prototype.dotAll ♪

    const text = '```\nЯ пожилой\nДракон\n```';
    const regex = /```(.*?)```/gs;
    console.log( Array.from(text.matchAll(regex), (x) => x[1].trim()) )

    I used it. String#matchAll and a regular expression with a fascinating submachine, as the left and right dividers match (```) Otherwise, it would be possible to use an expression with rear-view blocks. String#match: text.match(/(?<=```\s*).*?(?=\s*```)/gs) (this doesn't work if there's more than one match in the line).

    If any ECMAScript standard is to be supported, it can be used [^]:

    var text = '```\nЯ пожилой\nДракон\n```';
    var regex = /```([^]*?)```/g;
    var arr=[],m;
    while(m = regex.exec(text)) {
      arr.push(m[1].trim())
    }
    console.log(arr);

    In addition, use can be made [\s\S][\d\D] or [\w\W] to find any symbol. These designs are less effective, but they can be used when expressions can be used not only in the JavaScript environment, as they are supported in all regular NFA expressions.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2