如何使用JavaScript替换字符串中的所有点 [英] How to replace all dots in a string using JavaScript

查看:101
本文介绍了如何使用JavaScript替换字符串中的所有点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaScript字符串中替换所有出现的点(

I want to replace all the occurrences of a dot(.) in a JavaScript string

例如,我有:

var mystring = 'okay.this.is.a.string';

我想得到:好吧这是一个字符串

到目前为止,我尝试过:

So far I tried:

mystring.replace(/./g,' ')

但最终将所有字符串替换为空格。

but this ends up with all the string replaced to spaces.

推荐答案

你需要逃避,因为它具有正则表达式中的任意字符。

You need to escape the . because it has the meaning of "an arbitrary character" in a regular expression.

mystring = mystring.replace(/\./g,' ')

这篇关于如何使用JavaScript替换字符串中的所有点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆