以\\\作为替换值的未终止字符串常量



下面的代码正在进行一些替换。它不喜欢用\替换。引发的错误为Unterminated string constant。什么是正确的方法来解决这个问题,这样它就不会抛出错误

function matchLiteral(_ctx: InternalContext, descriptor: TokenDescriptor) {
const phrasePattern =
descriptor.item === ItemKey.MessageFieldValue
? "\S+"
: descriptor.phrase
.replace(/?/g, "\?") <- Good
.replace(/./g, "\.") <- Good
.replace(///g, "\/") <- Good
.replace(/\/g, "\") <- ** No Good
.replace(/(/g, "\(") <- Good
.replace(/)/g, "\)") <- Good
.toLowerCase()
.split(/s/)
.join("\s");
const pattern = new RegExp(`^${phrasePattern}(?=\s|$)`);

这是因为;用于对JS说您正在使用";作为字符而不是字符串的末尾所以你需要加上第四个\来表示";我想把\用作字符,而不是转义符">

最新更新