Home > Mobile >  Remove text between two strings including new line
Remove text between two strings including new line

Time:04-05

How can I remove text between two strings ( including new line , space , tab .. etc) using Regex in JavaScript. for example I want to remove the text between "START" and "ENDING"

This my text 
starting from the START
some Content 
some Content 
ENDING is the finish 
This is the conclusion 

my result should be

This my text 
starting from the is the finish 
This is the conclusion 

The Regex I am using replace( new RegExp('START(.*)ENDING','g'), ' '); does not work with new line .

CodePudding user response:

You may do the regex replacement with dot all mode enabled (use the /s flag):

var input = `This my text 
starting from the START
some Content 
some Content 
ENDING is the finish 
This is the conclusion`;

var output = input.replace(/\s START\b.*?\ENDING\s*/s, " ");
console.log(output);

CodePudding user response:

I found the answer in https://www.developerscloset.com/?p=548

var rx = new RegExp("START[\\d\\D]*?\ENDING", "g");
var result = myText.replace(rx, "");
  • Related