Explore the case-insensitive Regular Expressions and solutions.

Source: Internet
Author: User

Today, I encountered a case-insensitive Javascript Regular Expressions. I searched the internet and found myself a newbie.

In js, // g, where g indicates global match. Without this, it means that it will automatically end when only one match is performed; /I/indicates that the matching process is case insensitive. Haha .. This is useful in parameter filtering.

Copy codeThe Code is as follows:
Dxycode = dxycode. replace (/<br \ s * \/?> /Gi, '\ n ')

I can't help but take a look at the case where the regular expressions in. NET are case insensitive. In. NET, RegexOption enumeration can be used to modify the regular expression mode with the option that affects matching behavior. For example, IngoreCase is the case-insensitive limitation during the matching process. The attached code is as follows:

Regex. Match (String, String, RegexOptions );

Regex. Replace (String, RegexOptions)

Regular Expressions are actually very interesting, but they are not regular expressions. The following describes the problems I encountered today.

This problem may occur when javascript is used to bind data after ajax transmission, malicious page injection and malicious injection by other scripts are often caused by injection of malicious scripts and html tags that are not closed. The first solution we come up with at this time is to convert all tags into objects. In this way, the page will not be injected by malicious scripts, and there will be no wrong version. Good. This is a solution. After all, cainiao started to think about the problem. However, this will mistakenly kill a useful html Tag, such as img and. Therefore, the first idea is denied.

Then cainiao began to look for the second approach. First, let's solve the problem of malicious script injection. This is easy to handle, and some malicious scripts are filtered out. Well, this is simple. At this time, I used the regular expression I mentioned above. All the way down, malicious script injection is solved. The disadvantages of cainiao are not mentioned here. Let's look at the next question first. So how can we prevent incorrect page versions. Alas... This cainiao wants to break his head. The final conclusion is to use a regular expression to close the tag, or fix the tag that has not been closed, or directly Delete the tag that has not been closed.

Let's comment on cainiao's second solution and third solution. In doing so, there is a common determination. That is, you manually change the user's content. Casual users may think that your system is faulty because of a lack of good user experience. Second, regular expressions are always cost-effective and need to be constantly compared. Although the impact on the system may not be great, why not save performance?

In the end, cainiao found a good method for text box input, using the first method, the user's input content is directly converted to an object. Of course, if users need to input images and hyperlinks, we cannot help but satisfy them. How can this problem be met? The solution is as follows: Customize the tag and then convert it when it is displayed. At this time, the cainiao suddenly realized.

As a matter of fact, cainiao is constantly improving. The idea of solving this problem is: special handling of special problems. Some things cannot be popularized, so let's take special measures!

Finally, congratulations to cainiao for coming out of the misunderstanding of "regular expressions. Accompanied by regular expressions:

Http://www.jb51.net/tools/zhengze.html

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.