Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

javascript - 如何使用JavaScript替换字符串中的所有点(How to replace all dots in a string using JavaScript)

I want to replace all the occurrences of a dot( . ) in a JavaScript string(我想替换JavaScript字符串中所有出现的dot( . ))

For example, I have:(例如,我有:) var mystring = 'okay.this.is.a.string'; I want to get: okay this is a string .(我想得到: okay this is a string 。) So far I tried:(到目前为止,我尝试了:) mystring.replace(/./g,' ') but this ends up with all the string replaced to spaces.(但这最终将所有字符串替换为空格。)   ask by Omar Abid translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to escape the .(您需要逃脱.)

because it has the meaning of "an arbitrary character" in a regular expression.(因为它在正则表达式中具有“任意字符”的含义。) mystring = mystring.replace(/./g,' ')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...