javascript字符串用'

时间:2019-04-22 16:42:05

标签: javascript regex

这似乎是一个简单的问题,但我似乎无法在任何地方找到解决方案(或可行的解决方案)。

我有以下字符串:

“ REDEDACTED是REDEACTED,即REDEACTED,它专注于REDEACTED的研究和开发。 它拥有3个主要品牌:REDEACTED和REDEACTED。它的最后一个品牌是REDEACTED系列,该产品将出售给REDEACTED办事处。 通过其子公司REDEACTED和REDEACTED,REEDACTED可以通过位于REDEACTED的两家公司的设施来种植,加工和开发REDEACTED。 尽管它尚未出售任何产品,但它计划在不久的将来巩固合作伙伴关系。 “

,而我正在尝试将所有’实例替换为“。

我已经尝试过

 newText = newText.replace("/'/gi", "'")
 newText = newText.replace("’", "'")
 newText = newText.replace("/’/gi", "'")

没有真正的运气,有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您是否尝试过通过删除'的方式?

const regex = /’/ig;
const str = `REDEACTED is a REDEACTED, REDEACTED, which focuses on the research and development of REDEACTED. It has 3 main brands, REDEACTED, a REDEACTED, and REDEACTED, a therapeutic line. It’s last brand is a REDEACTED line, which is set to be sold to REDEACTED offices. Through its subsidiaries, REDEACTED and REDEACTED, REDEACTED is able to cultivate, process and develop its REDEACTED through the 2 company’s facilities, which are located in REDEACTED. Though it hasn’t sold any of its product yet, it plans to solidify partnerships in the near future.`;
const subst = `'`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log("Substitution result:\n", result);

答案 1 :(得分:-1)

我猜想在使用真正的正则表达式时,您需要删除第一个参数中的单引号。认为这可以解决问题:newText = newText = newText.replace(/’/g, "'")