javascript - 如何在不使用 `(?
我知道(?<!)
operator 背后的负面形象,但 Expo/React-Native 中使用的 Javascript 引擎似乎不支持它。我想要的是实现以下
export function processEmbedded(text: string): string {
return text.replace(/(?<!!)\[Embedded\]/
问题描述
我知道(?<!)
operator 背后的负面形象,但 Expo/React-Native 中使用的 Javascript 引擎似乎不支持它。我想要的是实现以下
export function processEmbedded(text: string): string {
return text.replace(/(?<!!)\[Embedded\]/gm, "![Embedded]");
}
我所做的有点笨拙,因为我剥离并重新添加了。
export function processEmbedded(text: string): string {
return text
.replace(/!\[Embedded\]/gm, "[Embedded]")
.replace(/\[Embedded\]/gm, "![Embedded]");
}
对于我的情况,它确实有效,但我很确定在极端情况下它不起作用。
您可以使用捕获组来使用此变通解决方案:
export function processEmbedded(text: string): string {
return text.replace(/(^|[^!])\[Embedded\]/gm, "$1![Embedded]");
}
(^|[^!])
匹配行首或不在!
第一个捕获组中的字符。在替换中,我们使用$1
反向引用将其放回原处。
解决方案
您可以使用捕获组来使用此变通解决方案:
export function processEmbedded(text: string): string {
return text.replace(/(^|[^!])\[Embedded\]/gm, "$1![Embedded]");
}
(^|[^!])
匹配行首或不在!
第一个捕获组中的字符。在替换中,我们使用$1
反向引用将其放回原处。
推荐阅读
- julia - 如何显示元组{Float64, Bool}
- text - 获取文本 SwiftUI 的剩余部分
- java - hashMap 中的值排序
- python - 在多项式基础上应用 Gram-Schmidt 正交化
- javascript - 如何在 ASP.NET 项目的 JavaScript 文件中放置发布日期和时间
- javascript - 单击href`#`时更改书签的url
- plot - 绘制线性函数 - 未定义变量:使用
- python - 在python上将一列拆分为多列
- dll-injection - 为什么EasyHook的RhInjectLibrary函数在创建进程后会导致目标进程崩溃
- sql - 如果条件被证明为真,则创建列
我知道(?<!)
operator 背后的负面形象,但 Expo/React-Native 中使用的 Javascript 引擎似乎不支持它。我想要的是实现以下
export function processEmbedded(text: string): string {
return text.replace(/(?<!!)\[Embedded\]/
问题描述
我知道(?<!)
operator 背后的负面形象,但 Expo/React-Native 中使用的 Javascript 引擎似乎不支持它。我想要的是实现以下
export function processEmbedded(text: string): string {
return text.replace(/(?<!!)\[Embedded\]/gm, "![Embedded]");
}
我所做的有点笨拙,因为我剥离并重新添加了。
export function processEmbedded(text: string): string {
return text
.replace(/!\[Embedded\]/gm, "[Embedded]")
.replace(/\[Embedded\]/gm, "![Embedded]");
}
对于我的情况,它确实有效,但我很确定在极端情况下它不起作用。
您可以使用捕获组来使用此变通解决方案:
export function processEmbedded(text: string): string {
return text.replace(/(^|[^!])\[Embedded\]/gm, "$1![Embedded]");
}
(^|[^!])
匹配行首或不在!
第一个捕获组中的字符。在替换中,我们使用$1
反向引用将其放回原处。
您可以使用捕获组来使用此变通解决方案:
export function processEmbedded(text: string): string {
return text.replace(/(^|[^!])\[Embedded\]/gm, "$1![Embedded]");
}
(^|[^!])
匹配行首或不在!
第一个捕获组中的字符。在替换中,我们使用$1
反向引用将其放回原处。