Home  >  Article  >  Web Front-end  >  How to Replace Dots in Strings with JavaScript Without Replacing All Characters?

How to Replace Dots in Strings with JavaScript Without Replacing All Characters?

DDD
DDDOriginal
2024-10-21 20:23:29204browse

How to Replace Dots in Strings with JavaScript Without Replacing All Characters?

Replacing Dots in Strings with JavaScript

Problem:

You wish to remove all instances of periods (.) from a JavaScript string. For example, transforming "okay.this.is.a.string" into "okay this is a string."

Attempted Solution:

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

However, this attempt replaces all characters in the string with spaces.

Solution:

The period (.) has a special meaning in regular expressions, representing "any character." To match a literal period, it must be escaped with a backslash:

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

The above is the detailed content of How to Replace Dots in Strings with JavaScript Without Replacing All Characters?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn