【问题标题】:Replace commas, full stops, hyphens, and spaces with underscore [duplicate]用下划线替换逗号、句号、连字符和空格 [重复]
【发布时间】:2020-09-05 18:40:54
【问题描述】:

我正在尝试用下划线替换逗号、句号、连字符和空格。我已经尝试了以下但得到错误Range out of order in character class

'Helo,._cool '.replace(/[,.- ]/g, "_")

【问题讨论】:

  • .- 被解释为从. 到空格的范围,这是没有意义的。

标签: javascript regex


【解决方案1】:

您需要对- 字符进行转义,因为它用于指定一系列字符,例如0-9a-z

console.log('Helo,._cool '.replace(/[,.\- ]/g, "_"));

【讨论】:

    猜你喜欢
    • 2012-07-14
    • 2021-12-07
    • 1970-01-01
    • 2018-06-11
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多