Regular expressions do not distinguish between case and approach. _ Regular Expressions

Source: Internet
Author: User
Tags html tags

Today met a eliminate JS regular expression case-insensitive problem, online a search, found himself is a rookie.

In JS inside//g, where G represents a global match, without this, it means that only one match will automatically end;/i/means that the matching process is case-insensitive. Oh.. This is useful in the process of parameter filtering.

Copy Code code as follows:

Dxycode=dxycode.replace (/<br\s*\/?>/gi, ' \ n ')

All said about this, naturally can't help but look at it. NET is not case-sensitive in the case of regular expressions. In. NET, the Regexoption enumeration can modify the regular expression pattern using options that affect the matching behavior. For example, the ingorecase of the matching process is to ignore the case limit. The attached code is as follows:

Regex.match (String, String, RegexOptions);

Regex.Replace (String, String, String, RegexOptions)

Regular expressions are actually a very interesting thing, but not positive. Here's a description of the problem I had today.

You may be faced with the problem, after the transfer of Ajax, the use of JavaScript binding data, often because of the injection of malicious script, HTML tags are not closed and other problems caused by the wrong version of the page and the malicious injection of others script. The first solution we think of this time is to convert all the tags into entities. This way the page will not be injected by malicious script, and the wrong version will not occur. Good, this is a way to solve, after all, rookie began to think about the problem. But it would have killed a useful HTML tag, such as Img,a and so on. Therefore, the first idea was denied.

Then the rookie starts looking for the second way. First decompose the problem, let's solve the problem of malicious script injection first. This is a good place to filter out part of the malicious script. Well, this is simple, and this time I used the regular expression I said above. All the way down, malicious script injection is resolved. First of all, do not point out the shortcomings of this rookie. Let's take a look at the next question. So how to prevent the page wrong version of the problem. Oh... This rookie is trying to break his head, the final conclusion is to use regular expression bar for the closure of the label, or patch the closed tag, or directly delete the closed tag.

Review rookie's second solution and the third way. There is a common determination to do so. Is that you manually change the user's content, casual users will also think that you have a system problem, because the lack of a good user experience. Second, regular expression is always a cost can be a dongdong, need to keep the comparison. Although the impact on the system may not be very large, but can save performance why not save it?

Finally rookie found a good way, for the text box input, the first method, the user's input directly into the entity. Of course, if the user has the need to enter pictures and hyperlinks, we can not afford to meet. How to meet? The solution is as follows: Customize the label and then display the custom label conversion. At this time the rookie suddenly realized.

In fact, rookie also in constant progress, the problem of the solution to experience a thought is: special problems special treatment of the idea. Some things can not be popularized, then we have special treatment of it!

Finally, congratulations rookie out of the "due to regular and regular" misunderstanding. Also attached are the regular expression encyclopedia:

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.