1
votes

I have the following sentence:

The quick brown fox jumps over the lazy dog (see Book 1). The quick brown fox jumps over the lazy dog (see above). The quick brown fox jumps over the lazy dog (see Book 2). The quick brown fox jumps over the lazy dog (see Book 3).

I need to remove text between parentheses - parentheses and outer whitespaces included - inside the sentence at the following conditions:

  • the text between parentheses contains the word Book;
  • the text between parentheses is not placed at the end of the sentence.

So that sentence should become:

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog (see above). The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog (see Book 3).

I know this regex sentence.replace(/ *\([^)]*\) */g, ""); - but I don't know how to apply the above-mentioned conditions.

2
This question needs more research and a bit of direction. For example, you could write a tokenizer or you could just use simple substitution with a regex. Additionally, using large latin example makes it harder to read. It also looks like your example doesn't follow description. Take a look at stackoverflow.com/help/how-to-ask - zhon
I didn't down vote so I am only speculating on why you got a down vote (it costs the down voter reputation). As for clarity of your question, you got two answers which both worked correctly with your example and yet didn't satisfy question. Questions get upvoted when they are easily and quickly understood (think 30 secs or less to understand). Hope this helps. - zhon

2 Answers

1
votes

Another regex:

const regex = /\s\([^)]*?Book.*?\)(?=.*\(.*?Book.*?\))/g

In the form of JS example:

  const p = 'Text (see the Book "def"). Text (see above). Text (Book 2) with more text (Book 3).';

  console.log(p.replace(regex, ''));
1
votes

Here, hope it will help you

const sentence = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit (see the Book "abc"). Quisque non ante nisi (see the Book "def"). Nam id justo velit (see above). Donec hendrerit dui sit amet tempus molestie (see the Book "ghi").'

console.log(sentence.replace(/\s?\([\w\s\"]*?Book[\w\s\"]*?\)\s?(?=..*\w)/gm, ''));

And here you can see the explanation and experiment with the regexp - https://regex101.com/r/hOZg0g/5