Home  >  Article  >  Web Front-end  >  More efficient string replacement in javascript_javascript skills

More efficient string replacement in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:02:011002browse

First, let’s take a look at how it is done:

Copy the code The code is as follows:

function toTXT (str){
str = str.replace(/&/g, "& amp;");
str = str.replace(/>/g, ">");
str = str.replace(/ str = str.replace(/"/g, """);
str = str.replace(/ '/g, "& #39;");
return str;
}
[/code
Analysis: The above method is used to replace the HTML code of the filter string. I have always thought that This is very inefficient because it requires five full-text matches (not full-text searches in the database). Is there a way to replace different strings with different results using only one full-text match?

Hehe, I finally found the following method:

[code]
function toTXT(str){
var RexStr = /<|>|"|'| &/g
str = str.replace(RexStr,
function(MatchStr){
switch(MatchStr){
case "<":
             return "<";
                                                                                              break;
case """:
return "& quot;";
break ;
case "'":
return "& #39;";
case "&":
return "&";
break;
                                                                : 




It seems a little more complicated than the first method and requires a little more code to write. However, it only needs to be used once to replace the matching characters in the entire string with different ones. The result is very efficient. And the code looks very intuitive and easy to modify.

What’s more important is that if you want to replace "&" in the first method, it must be placed at the front, and there is no need to worry about this problem in the following aspects.

JavaScript has many little-known uses, and many ideas are not found in other languages. No matter how powerful JAVA is, its regular expressions were only introduced in JDK 1.4 and lag behind a lot. But I didn’t say that JAVA is definitely inferior to JS in terms of class.

The scope of application of JS is definitely not limited to HTML, it is also used in many other aspects, such as WebFT (a tool for testing websites), .NET, etc., and it will be released soon, the legendary "FLASH killer" - —After the emergence of WPF/E (Windows Presentation Foundation/Everywhere), the application scope of JS has become wider.

Many people disdain JS, thinking it is very low-level and does not even have basic object types. NO, that's wrong. In .NET, that is, Jscript.Net, Microsoft has upgraded the version of JS to 8.0. Basically, it has no difference from C#, and it has everything that a programming language should have.

To tell you a joke, I use Jscript when writing .net and ASP, but I don’t fall into the category of talking about VB and C# in general, but I think if I can write in one language, It will be a very pleasant thing to go to the front, including his peripheral projects. Never juggle between multiple languages ​​again.
Let’s take a look at the power of WPF/E (Windows Presentation Foundation/Everywhere):
At the 2005 PDC conference, Microsoft introduced a project called WPF/E (Windows Presentation Foundation/Everywhere) Everywhere) technology showed new features at its own MIX'06 conference. WPF/E relies on XAML and JavaScript to create web pages. It also comes with cross-platform tools and supports code from CLR, .NET, C#, VB.NET, etc. In essence, it is a tool very similar to Flash that currently belongs to Adobe. .

Microsoft programmer Mike Harsh once said: "Yes, we are bringing C# to Mac."

Although it is considered a "Flash killer", Microsoft's goal is more Go big: let users use Microsoft software and technology not only on desktop systems, but also on the web and mobile devices.

"In essence, WPF/E is not a copied work at all. It provides developers, designers, and technicians with a comprehensive development and design platform." Developer Joe Stegman said, "It can do a good job It can be integrated with other Microsoft products, which is beyond the reach of the Flash platform.” Stegman said that they have been working hard on WPF/E development and have released several internal versions, and the user experience version will be released soon.

Although Flash is now very popular, some people may think that Microsoft's WPF/E cannot surpass Flash, but think about Netscape's Netscape many years ago...

The picture is demonstrated in the browser Vector graphics, you can scale and rotate the graphics at will

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn