Backward reference: a regular expression uses parentheses () to store the matching information in a temporary buffer. You can use '\ n' to access the buffer, where N is a one-or two-digit decimal number that identifies a specific buffer. If there is a pair in the expression, the buffer will have multiple results.
Can I use non-captured metacharacters? :,? = ,?! To ignore the save of the matching.
Q: What is the impact of not ignoring the storage of this buffer? When should I ignore the storage?
A: The usage of a regular expression () is called backward reference or reverse reference. This means that the matching results of a regular expression are temporarily stored for future calls.
Example 1:
var num = "1234 5678";var newNum = num.match(/(\d{4}) (\d{4})/);console.log(RegExp.$2,RegExp.$1); 5678 1234
Example 2:
VaR num = "1234 5678"; var newnum = num. Match (/(? : \ D {4}) (\ D {4})/); console. Log (Regexp. $2, Regexp. $1); 5678 null
Example 3: You can also use '\ n' in the expression to retrieve the cached result.
var num = "dotor dogdog";var newNum = /(?:dot)or (dog)\1$/.test(num);console.log(newNum); //trueconsole.log(RegExp.$1); //dog
It can be seen that the cache result is the result of matching () from left to right of the regular expression, starting from $1 to $99
That is to say, if we do not need this cache result later, we can use the non-capture mode (? :); This is when "or" is used
Is very useful. Example: 'industr (? : Y | ies) 'is a simpler expression than 'industry | industries'.
-------------------------------------------------------------------------------- Gorgeous split line ------------------------------------------------------------------------------------
After learning about the captured element, let's take a look at the two pre-checks:
? = Forward pre-query: match the search string at any position that begins to match the Regular Expression Pattern in parentheses
?! Negative pre-query: matches the search string at any position that does not match the regular expression pattern.
The above concepts may be difficult to understand. I like to use examples to make myself convinced:
? = Forward pre-query, also called forward lookup. For example:/Windows (? = 95 | 98 | nt | 2000) // It can match "windows in Windows 2000" but cannot match windows in "Windows 3.1.
?! Reverse pre-query. The result is the opposite. It can match windows in Windows 3.1, but cannot match windows in Windows 3.1.
Forward pre-query and reverse pre-query do not consume characters. In the beginning, I didn't understand what this meant. Until I saw an example of a big bull on the internet, I was overwhelmed instantly ..................
var text = "abc";var reg = /a(?=b)bc/;var reg2 = /a(b)bc/;var result = reg.test(text);var result2 = reg2.test(text);console.log(result); //trueconsole.log(result2); //false
It can be seen that forward matching does not consume characters B, and B can be matched later.
Finally, leave a regular RegEx for parsing the URL. If you understand this, you will actually understand the Back-to-Back Reference and pre-check it in reverse direction.
var url = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu#top";var parse = url.match(/^(?:([a-z]+):\/\/)?([\w-]+(?:\.[\w-]+)+)?(?::(\d+))?([\w-\/]+)?(?:\?((?:\w+=[^#&=\/]*)?(?:&\w+=[^#&=\/]*)*))?(?:#([\w-]+))?$/i);console.log(parse);
Note: Why is my blog so ugly? It's hard for everyone. I have time to optimize it.
Use of parentheses in a regular expression-also called backward reference