javascript - 删除括号之间的文本,但仅在给定条件下
问题描述
我有以下句子:
敏捷的棕色狐狸跳过懒惰的狗(见第 1 册)。敏捷的棕色狐狸跳过懒狗(见上文)。敏捷的棕色狐狸跳过懒惰的狗(见第 2 册)。敏捷的棕色狐狸跳过懒惰的狗(见第 3 册)。
我需要在以下条件下删除句子内的括号之间的文本 - 包括括号和外部空格:
- 括号之间的文本包含单词Book;
- 括号之间的文字没有放在句末。
所以那句话应该变成:
敏捷的棕色狐狸跳过了懒狗。敏捷的棕色狐狸跳过懒狗(见上文)。敏捷的棕色狐狸跳过了懒狗。敏捷的棕色狐狸跳过懒惰的狗(见第 3 册)。
我知道这个正则表达式sentence.replace(/ *\([^)]*\) */g, "");
- 但我不知道如何应用上述条件。
解决方案
在这里,希望对你有帮助
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, ''));
在这里你可以看到正则表达式的解释和实验 - https://regex101.com/r/hOZg0g/5
推荐阅读
- javascript - 如何使用正则表达式删除字符串中所有出现的重复字母。在 JS 中
- laravel - Laravel Passport 覆盖主键不起作用
- ios - SwiftUI:在用户滑动以关闭时显示警报
- scala - 将 Type1:EitherT[Future, String, Int] 转换为 Type2:EitherT[Future, String, Option[Int]] 而 Type1 中的所有 Left 在 Type2 中变为 Right(None)
- php - 带有参数日期格式的执行存储过程出错
- powershell - 在每个自己的 Out-GridView 中显示每个 AD 组的 AD 组成员
- javascript - 检查javascript对象是否有密钥,如果它确实用新密钥替换它
- javascript - React类组件中的Javascript“无法读取未定义的属性”
- c# - 使用 Visual Studio 2019 C# 将静态方法重构为扩展方法
- javascript - 如何防止网站加载到网页的特定部分