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.