The solution to the replace problem of JavaScript (JS)

Source: Internet
Author: User
I am the front-end layman, JS I use less. Today, accidentally found that JS comes with the replace "incredibly" replaced only 1, and many other languages are replaced by all. You might say, cut, I've known. Master please take a detour. You might say that you can solve this problem with JS's regular. Yes, that's right, but I am has a "don't use regular" hobby. So come up with a way, since JS you only help me do once, I will loop let you do many times, the code is written by others:
function ReplaceAll (str, sptr, SPTR1) {while   (Str.indexof (sptr) >= 0) {      str = str.replace (sptr, SPTR1);   }   return str;}

Glad to find a solution. But the post code says "I don't feel like it", and here's an example: ReplaceAll (A, ' B ', ' BC ') will die. I'm sweating! I know it's going to be a dead loop without running. Disappointed. Later, I thought of a way:

function ReplaceAll (str, sptr, sptr1) {    var ps = str.split (sptr);    var s2 = ps.join (SPTR1);    return S2;}

Put it up for your master to see if this approach is flawed.

I'd like to write a memo.

The solution to the replace problem of JavaScript (JS)

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.