简体   繁体   中英

Replace multiline text between two strings

I need to replace old value between foo{ and }bar using Javascript regex.

foo{old}bar

This works if old is a single line:

replace(
    /(foo{).*(}bar)/,
    '$1' + 'new' + '$2'
)

I need to make it work with:

foo{old value
which takes more
than one line}bar

How should I change my regex?

Change your regex to,

/(foo{)[^{}]*(}bar)/

OR

/(foo{)[\s\S]*?(}bar)/

so that it would match also a newline character. [^{}]* matches any character but not of { or } , zero or more times. [\\s\\S]*? matches any space or non-space characters, zero or more times non-greedily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM