【问题标题】:How to replace periods with X? [duplicate]如何用 X 替换句点? [复制]
【发布时间】:2020-01-06 14:06:41
【问题描述】:

var mystring = "this.is.a.test";
mystring = mystring.replace(/./g, "X");
console.log(mystring);

我期待thisXisXaXtest 的输出,但我的日志显示XXXXXXXXXXXXXX

【问题讨论】:

标签: javascript


【解决方案1】:

. 是正则表达式中的特殊字符(表示匹配除新行之外的任何内容),您需要对其进行转义

var mystring = "this.is.a.test";
mystring = mystring.replace(/\./g, "X");
console.log(mystring);

DOT

【讨论】:

    【解决方案2】:

    . 匹配除换行符以外的所有字符。要按照您的意图使用它,需要对其进行转义:\.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 2013-01-09
      • 2012-12-14
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      相关资源
      最近更新 更多