Solve the problem that replace only sets the first matching string in JavaScript.

Source: Internet
Author: User

Today, my colleague asked him that when he used replace to process strings on the client side, when the result was found, there was only the first matching string, none in the background. Example: for example, Xiao Miao wants to convert the strings of two AA strings into AA false settings.ArticleThree of them need to be set as follows:

In this article, AA sets AA as an Example

Compile the program example:

VaR S = S. Replace ('A', 'A ');

At first glance, there seems to be no problem in the program. However, I checked the replace usage in Javascript. In fact, the first response should be in regx mode.

Therefore

VaR S = S. Replace (// \ The String/g of the token you want to set, 'A ');

In this way, all matching strings can be correctly set to complete !!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.