Home > Web Front-end > JS Tutorial > body text

What kind of regular expression can display highlight?

php中世界最好的语言
Release: 2018-01-08 10:36:03
Original
2106 people have browsed it

What kind of regular expression will I bring to you this time? can display highlight. How can I use regular expression to display highlight? What are the precautions for regular expression display highlight? The following is a practical case, let’s take a look.

When programmers write code, they must deal with

string and "query". There is something called a regular expression at the intersection of the two. If you use this guy well, you can improve your programming efficiency. If you don't use it well... you can learn it first.

Regarding the use of regular expressions, here is a simple example:

var m = location.href.match(/(\w+:)\/{0,3}([^\/]+)(?:(\/[^\?#]*))?(?:(\?[^#]+|.+))?(?:(#.*))?/);
var res = {
    protocol: m[1],
    host: m[2],
    path: m[3],
    search: m[4],
    hash: m[5]
};
console.log(res);
Copy after login

After holding it for a few minutes = =||, I also feel that this regular expression is not particularly easy to understand (it is red and black, it should be slightly Be clear). Some friends may not know what (?:content) means. There is also knowledge about grouping and foresight similar to (?!content) and (?=content). It is recommended to ask Brother Du and Gu Niang.

1. Small DEMO example of regular application

The following is a string of handwritten codes, placed in textarea:

/*** @author barret lee* @date   2013-10-06* @email  barret.china@gmail.com*/
//outer varvar a = "this id outer string";
//closurefunction b() { //inner var var a = "this is inner string"; var g = a.replace(/this is inner string/g, function() {  return new Function("/*clousure*/this.a")(); });
 /** * @description closure - regExp test * @author barret lee */ function c() {  return {   a: a,   g: g  } }
 return c;}
var s = b()(); //s.a, s.g
Copy after login

Regular matching, processing the above pile Small DEMO of string:

/*** @author barret lee* @date 2013-10-06* @email barret.china@gmail.com*/
//outer varvar a = "this id outer string";
//closurefunction b() {    //inner var    var a = "this is inner string";    var g = a.replace(/this is inner string/g, function() {        return new Function("/*clousure*/this.a")();    });
    /**    * @description closure - regExp test    * @author barret lee    */    function c() {        return {            a: a,            g: g        }    }
    return c;}
var s = b()(); //s.a, s.g
Copy after login

is similar to many

front-end templates, artTemplate, baiduTemplate, etc. are all excellent applications of regular expressions. It is worth taking a look at the source code and studying them carefully. Taking into account those easy-to-error points, the source code is not long, only two to three hundred lines. It is probably easier to understand one, but when you do it yourself, you will always find that many details cannot be handled well. I spent a long time fiddling with the above regular expressions = =

regular expressions, which are quite convenient to use. of. Regular skills are worth having~

I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

How to use jquery's ajax to asynchronously submit form data

How to implement springmvc to receive the array submitted by jquery Then extract the data

Detailed explanation of vue.js syntax and common instructions

The above is the detailed content of What kind of regular expression can display highlight?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template