Regex to match all the strings between two identical strings

后端 未结 2 994
醉梦人生
醉梦人生 2020-12-02 02:52

E.g. I have this string -- This -- is -- one -- another -- comment -- I want the matched elements to be \"This\", \"is\", \"one\", \"another\", and \"comment\"

相关标签:
2条回答
  • 2020-12-02 03:18

    In general, you need to use a pattern like

    STRING([\s\S]*?)(?=STRING|$)
    

    It will match STRING, then capture into Group 1 any zero or more chars, as few as possible, up to the first occurrence of STRING *stopping right before this word** because the (?=...) is a positive lookahead that, being a zero-width assertion, does not consume matched text or end of string.

    A generic variation of the pattern is

    STRING((?:(?!STRING)[\s\S])*)
    

    It uses a tempered greedy token, (?:(?!STRING)[\s\S])*, that matches any char, 0 or more occurrences, that does not start a STRING char sequence.

    To get all the substrings in the current solution, use a lookahead like

    /--\s+([\s\S]*?)(?=\s+--)/g
                    ^^^^^^^^^
    

    See the regex demo.

    Note that [^--]+ matches 1 or more symbols other than a -, it does not match any text that is not equal to --. [...] is a character class that matches a single character. To match any text of any length from one char up to the first occurrence of a pattern, you can rely on a [\s\S]*? construct: any 0+ chars, as few as possible (due to the lazy *? quantifier).

    JS demo:

    var s = '-- This -- is -- one -- another -- comment --';
    var rx = /--\s+([\s\S]*?)(?=\s+--)/g;
    var m, res=[];
    while (m = rx.exec(s)) {
      res.push(m[1]);
    }
    console.log(res);

    0 讨论(0)
  • 2020-12-02 03:25

    To read all I would use positive look achead:

    const data = '-- This -- is -- one -- another -- comment --'
    
    const readAll = data => {
      const regex =/--\s*(.*?)\s*(?=--)/g
      const found = []
      let temp
      while (temp = regex.exec(data)) {
        found.push(temp[1])
      }
      return found
    }
    
    console.log(readAll(data))

    And to remove comments just do this:

    const data = `-- This -- is -- one -- another -- comment -- this is not a comment`.replace(/--.*--/g, '')
    
    console.log(data)

    0 讨论(0)
提交回复
热议问题