最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

regex - Create array of regexp match(multiline, javascript) - Stack Overflow

matteradmin1PV0评论

I want to create array of javascript's regexp match string from the text below.

.root
 this is root
..child 1
 this is child 1
 this is also child 1's content.
...child 1-1
 this is child 1-1

..child 2
 this is child 2
...child 2-1
 this is child 2-1
.root 2
 this is root 2

and desired array is below

array[0] = ".root
 this is root"

array[1] = "..child 1
 this is child 1
 this is also child 1's content"

array[2] = "...child 1-1
 this is child 1-1
"

array[3] = "..child 2
 this is child 2"

array[4] = "...child 2-1
 this is child 2-1"

array[5] = ".root 2
 this is root 2"

In Java, I can do like ^\..*?(?=(^\.|\Z)), but in Javascript there is no \Z, . doesn't match newline character, and $ matches newline character (not just the end of string).

How can I achieve this array?
I use this site ( ) to test regexp.

I want to create array of javascript's regexp match string from the text below.

.root
 this is root
..child 1
 this is child 1
 this is also child 1's content.
...child 1-1
 this is child 1-1

..child 2
 this is child 2
...child 2-1
 this is child 2-1
.root 2
 this is root 2

and desired array is below

array[0] = ".root
 this is root"

array[1] = "..child 1
 this is child 1
 this is also child 1's content"

array[2] = "...child 1-1
 this is child 1-1
"

array[3] = "..child 2
 this is child 2"

array[4] = "...child 2-1
 this is child 2-1"

array[5] = ".root 2
 this is root 2"

In Java, I can do like ^\..*?(?=(^\.|\Z)), but in Javascript there is no \Z, . doesn't match newline character, and $ matches newline character (not just the end of string).

How can I achieve this array?
I use this site ( http://www.gethifi./tools/regex ) to test regexp.

Share Improve this question edited Jun 20, 2018 at 16:59 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Nov 2, 2011 at 4:44 yshrsmzyshrsmz 1,1792 gold badges15 silver badges34 bronze badges 1
  • By the way, $ will only match the end of string unless you add the m flag. For example, /foo$/m would match "foo" in "foo\nbar" but /foo$/ would not. – davidchambers Commented Nov 2, 2011 at 5:52
Add a ment  | 

2 Answers 2

Reset to default 6

text.split(/\r?\n^(?=\.)/gm) produces the same array.

text.match(/^\..*(\r?\n[^.].*)*/gm) ugly, but still.

Here it is in a single regular expression:

var re = /^[.][\s\S]*?(?:(?=\r?\n[.])|(?![\s\S]))/gm
var match
var matches = []
while (match = re.exec(text)) {
  matches.push(match[0])
}
console.log(matches)

outputs:

[
  ".root\nthis is root",
  "..child 1\n this is child 1\n this is also child 1's content.",
  "...child 1-1\n this is child 1-1\n",
  "..child 2\n this is child 2",
  "...child 2-1\n this is child 2-1",
  ".root 2\n this is root 2"
]

Some useful tricks:

  • use [\s\S] to match any character
  • use (?![\s\S]) to simulate \Z
Post a comment

comment list (0)

  1. No comments so far