JavaScript's more efficient string replacement _javascript tips

Source: Internet
Author: User
First look at the original how to do:
Copy Code code as follows:

function Totxt (str) {
str = str.replace (/\&/g, "& amp;");
str = str.replace (/\>/g, "& gt;");
str = str.replace (/\</g, "& lt;");
str = str.replace (/\ "/g," & quot; ");
str = str.replace (/\ '/g, "& #39;");
return str;
}
[/code
Analysis: The above method is used to replace the HTML code of the filter string. Always think this is inefficient, because in order to do this string 5 full text matching (not in the database of Full-text search), there is no way to use only one full text matching, you can replace different strings for different results?

Oh, finally found the following method:

[Code]
function Totxt (str) {
var rexstr =/\<|\>|\ "|\" |\&/g
str = str.replace (REXSTR,
function (MATCHSTR) {
Switch (MATCHSTR) {
Case "<":
return "& lt;";
Break
Case ">":
Return "& gt;";
Break
Case "\":
return "& quot;";
Break
Case "'":
Return "& #39;";
Break
Case "&":
Return "& amp;";
Break
Default:
Break
}
}
)
return str;
}



Looks more complicated than the first method, write a little bit more code, but, only once, you can replace the entire string inside the matching characters for different results, very efficient. And the code looks very intuitive, easy to modify.

More importantly, the first option is to replace the word "&" with the front end, and there is no need to worry about the problem at all.

JavaScript also has a lot of little-known usage, and many ideas are not in other languages. Even if the Java again how strong, his regular expression is in JDK1.4 inside only then introduced, behind a lot. But I did not say Java must be better than JS only class words ah.

JS's scope of application is absolutely more than HTML, there are many other aspects of the use of him, such as WEBFT (test site tools),. NET, and in the upcoming release, the legendary "Flash Killer"--wpf/e (Windows presentation foundation/everywhere) After the appearance, JS application scope is broader.

Many people disdain JS, think he is very low-level, even the basic object types are not. No, wrong, in. NET, that is, JScript.NET, Microsoft will be the version of JS to 8.0, basically he has no any difference with C #, all the program language should have him.

To say a joke, I write the. NET and ASP on the use of JScript, but VB and C # I do not belong to that kind of generalities, but I think that if you can use a language from the back to the front, including his peripheral projects, it will be a very pleasant thing. Never again in many languages.
look at the power of wpf/e (Windows presentation foundation/everywhere):
At the 2005 PDC conference, Microsoft introduced a technology called WPF/E (Windows presentation Foundation/everywhere), which featured new features at its own mix ' 06 meeting. WPF/E relies on XAML and JavaScript to make Web pages, as well as cross-platform tools that support the CLR,. NET, C #, and VB. NET and so on, in essence, it is a similar tool to the current Adobe Flash.

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

Although it is thought to be "Flash killer", Microsoft's goal is a little more ambitious: let users not only use Microsoft's software and technology on desktop systems, they will also occupy network and mobile devices.

"In essence, wpf/e is not a copy of the work, it provides developers, designers, technicians provide a comprehensive platform for development and design." "It's a good combination of Microsoft's other products," says developer Joe Stegman, the Flash platform. Stegman says they have been working very wpf/e development, and several internal versions have been released, and the user experience version will be released shortly thereafter.

Although Flash scenery is now infinite, some people may think that Microsoft's wpf/e can not surpass Flash, but think of Netscape's Netscape years ago ...

The figure is a vector graphic that is shown in the browser and can be scaled and rotated at will

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.