Home > Web Front-end > JS Tutorial > body text

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

DDD
Release: 2024-10-21 20:23:29
Original
203 people have browsed it

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!

source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!