Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to replace all strings with javascript

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to replace all strings with javascript". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to replace all strings with javascript" can help you solve the problem.

Js itself does not provide the replaceAll () method, so you need to write such a method to replace all strings. Several online writing methods are summarized as follows:

Method 1:

/ / create the replaceAll () function String.prototype.replaceAll = function (FindText, RepText) {return this.replace (new RegExp (FindText, "g"), RepText);} var str= "shingfkhshsnf"; / / usage, replace all n with w str= str.replaceAll ("n", "w") [xss_clean] (str)

Another way to write replaceAll is actually pretty much the same.

/ / another way of writing replaceAll is actually almost String.prototype.replaceAll = function (reallyDo, replaceWith, ignoreCase) {if (! RegExp.prototype.isPrototypeOf (reallyDo)) {return this.replace (new RegExp (reallyDo, (ignoreCase?)) "gi": "g"), replaceWith);} else {return this.replace (reallyDo, replaceWith);}} / / add, another simplified way to write var str = "dddd-dsss" / / replace the middle "-", as follows: var newStr = str.replace (new RegExp ('-', 'gm'),'')

Method 2:

/ / the replacement format is as follows: / / str.replace (/ string to be replaced / g, "new string"); var str= "shingfkhshsnf"; / / usage, replace all n with w str= str.replace (/ nUnip g, "w"); [xss_clean] (str)

Scene:

There is a very important scenario that will use this replacement function, that is, in actual development, the json string returned by the backend needs to be converted into json, but the direct conversion will fail because some Chinese strings have newline characters, which must be replaced before the format can be successfully formatted. Before formatting, you can copy the code to the online json formatting tool to try.

As follows:

/ / replace json newline operator JSON.parse (myJson.replace (/\ n g, "")) that's all for "how to replace all strings with javascript". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report