查找模式并仅删除几个字符

find pattern and remove few character only

本文关键字:几个 字符 模式 删除 查找      更新时间:2023-09-26

我试图使用 Nodejs 中可用的 javascript 方法搜索字符串中的模式,并在相同的模式中用其他字符替换几个字符。

例如:我正在搜索"*,*"任何括在双引号内且中间有逗号的东西我需要删除两者之间的双引号和逗号。

场景就像,我可以有多个字符串也用逗号分隔例如:123,234,"1,234.50",345,456

我只需要对那些在双引号内的模式执行此操作。所以转换后,它必须像 123,234,1234.50,345,456

如何在长文本的镜头中做到这一点?

您可以将replace()与捕获的组正则表达式一起使用

document.write(
  // get the values withing `""`
  '123,234,"1,234.50",345,456'.replace(/,"(.+?)",/g, function(m, m1) {
    // replace the `,` within the `""` and update
    return ',' + m1.replace(/,/g, '') + ',';
  })
)

带ES6箭头功能

document.write(
  '123,234,"1,234.50",345,456'.replace(/,"(.+?)",/g, (m, m1) => ',' + m1.replace(/,/g, '') + ',')
)