-3

I am looking for a javascript regex for deleting all lines between two words including the words. I could find something like this

Dim input = "one two three four START four five four five six END seven"
Dim output = Regex.Replace(input, "(?<=START.*)four(?=.*END)", "test")

This is for VB and in addition, it does not work for multiple lines and also delete both start and end.

How do I solve this problem?

glennsl
  • 28,186
  • 12
  • 57
  • 75
codeProtect
  • 39
  • 2
  • 5

1 Answers1

0

This expression can capture your undesired text in between start and end including new lines:

START([\s\S]*)END

enter image description here

RegEx Descriptive Graph

This graph visualizes the expression, and if you want, you can test other expressions in this link:

enter image description here

Basic Performance Test

This JavaScript snippet returns runtime of a 1-million times for loop for performance. You could simply remove the for and that might be what you may be looking for:

const repeat = 1000000;
const start = Date.now();

for (var i = repeat; i >= 0; i--) {
 const string = 'anything you wish before START four five four \n \n \n \n five six END anything you wish after';
 const regex = /(.*START)([\s\S]*)(.*END)/gm;
 var match = string.replace(regex, "$1 $3");
}

const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match  ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test.  ");

Edit

If you wish to replace one word in a string with new lines, this expression might be helpful:

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    Thanks Emma it working great. Unfortunately I do not have credit to give the thumbs up – codeProtect May 11 '19 at 12:00
  • 1
    Emma what happens if I want to change a specific word inside a multi-line text? For example change "football" to " boxing" in s="Here is football match. I want to replace football with something else that also consider to change footballA too" – codeProtect May 11 '19 at 15:17
  • 1
    Emma thanks. I found another issue. Assume that I have a Json that I read it as a string. Assume one of the keys of the Json is dog ( but it could be Dog or dOg). How could I remove that key and value and comma separator of that line with regular expression and javascript? For example : `{ "someElements":"Could be here","dog":"is an animal", "animals":{"blabla":"blabla"}}` So I want to change it to `{ "someElements":"Could be here","animals":{"blabla":"blabla"}}` Thanks! – codeProtect May 11 '19 at 20:46
  • 1
    @codeProtect you can [accept an answer](https://meta.stackexchange.com/a/5235/289255) by clicking on the grey checkmark on the left side of the answer – adiga May 12 '19 at 06:03