gpt4 book ai didi

javascript - 从字符串中删除点和空格

转载 作者:行者123 更新时间:2023-12-04 08:45:58 26 4
gpt4 key购买 nike

我想用正则表达式 . 删除点 和空格 text.replace(/[ .]+/g, '')

This is an 8-string 12.34.5678; and this is another 13-string 1234 5678 9123 0 okay?


但主要问题是它从句子中删除了所有点和空格。

Thisisan8-string12345678;andthisisanother13-string1234567891230okay?


  • 8 字符串 12.34.5678
  • 另一个 13 字符串 1234 5678 9123 0

  • 需要转换为。
  • 8 字符串 12345678
  • 另一个 13 字符串 1234567891230

  • 所以句子将是:

    This is an 8-string 12345678; and this is another 13-string 1234567891230 okay?


    我究竟做错了什么?我坚持寻找/匹配正确的解决方案。

    最佳答案

    您可以使用

    s.replace(/(\d)[\s.]+(?=\d)/g, '$1')
    s.replace(/(?<=\d)[\s.]+(?=\d)/g, '')
    regex demo .
    详情
  • (\d) - 组 1(替换模式中的 $1 是组的值):一个数字
  • [\s.]+ - 一个或多个空格或 .字符
  • (?=\d) - 确保下一个字符是数字的正向前瞻。

  • 参见 JavaScript 演示:

    const text = 'This is an 8-string 12.34.5678; and this is another 13-string 1234 5678 9123 0 okay?';
    console.log(text.replace(/(\d)[\s.]+(?=\d)/g, '$1'));

    关于javascript - 从字符串中删除点和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64325294/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com