Home > Software design >  Get two string from a multilines data in javascript
Get two string from a multilines data in javascript

Time:03-12

I tried to get 2 string from multilines data. I used Reguler Expression to match the string that I want. First string is just one line, but the second is multipleline. This is my RegEx function

const RegExp = /\/Data first:(.*) second:(.*\n.*)/
const first = RegExp.exec(text)[1]
const second = RegExp.exec(text)[2]

I have the following string

/Data first:this first must include second:this second and
third
fourth
.
.
.
and so on must include

But it's not working as I expected. What I got is like this

first: this first line
second: this second
third

But I need to get result is like this

first: this first line
second: this second
third
fourth
.
.
.
and so on

I tried a lot of things, but I can't figure it out.

CodePudding user response:

You can try this \/Data first:(.*) second:(.*[\s\S]*). I tested with your data and it's working as your requirement. You can try this playground.

https://regex101.com/r/a4bJIH/1

  • Related