Share experiences on using the Replace method in JS, and share experiences with replace

Source: Internet
Author: User

Share experiences on using the Replace method in JS, and share experiences with replace

Recently, I checked a bug because of the Replace method in JS. When a string needs to be replaced, the Replace method in JS is generally used, if the first parameter of the Replace method is a passed string, it will only Replace the first parameter. The Code is as follows:

Copy codeThe Code is as follows:
Var str = "0cea65d5-db8e-4406-a6f8-c88ac7f0e185, E846C244-8A19-4374-879B-0B1DC08D1747, 6CB3EBA4-1E22-4E4D-8800-AE31130B6F5D ";
Alert (str. replace (",","','"));

The above code is intended to replace the comma (,) of the GUID separated by commas (,) with ',', but the actual result is to replace the first comma.

To solve this problem, you only need to use the regular expression of the first replace parameter. The Code is as follows:

Copy codeThe Code is as follows:
Var reg = new RegExp (",", "g ");
Var str = "0cea65d5-db8e-4406-a6f8-c88ac7f0e185, E846C244-8A19-4374-879B-0B1DC08D1747, 6CB3EBA4-1E22-4E4D-8800-AE31130B6F5D ";
Alert (str. replace (reg ,"','"));

The result is as follows:

The above is all the content of the text, hoping to help you learn javascript.

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.