怎么使用 .replace() 方法将字符串 “Hello” 中的 “” 替换为?

huangapple go评论50阅读模式
英文:

How to replace the "" in "Hello" with .replace()?

问题

This only replaces the first ", but not the one at the end of a word

partFormatted = partFormatted.replace(""", "");

英文:

This only replaces the first ", but not the one at the end of a word

partFormatted = partFormatted.replace("\"", "");

答案1

得分: 1

使用正则表达式是在其他帖子中没有看到的内容。
具体来说,是全局标志。

partFormatted = partFormatted.replace(/"/g, "");

'g' 表示它应该找到所有给定的匹配项,并用给定的输入替换。

英文:

Something I didn't see in the other post is using a regular expression.
Specifically the global flag.

partFormatted = partFormatted.replace(/"/g, "");

The 'g' indicates it should find all given matches and replace with given input

huangapple
  • 本文由 发表于 2023年5月17日 16:05:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定