DWQA QuestionsCategory: ProgramJavaScript regular interception of the second / latter content requires optimal performance
Jackie Kozan asked 1 week ago

problem
Regular Interception of the Second/Later Content
For example/test/gagaga
Gagaga
Another example is/haha/yr-1-k
Get yr-1-k
What is uncertain is the second / the following: there may be English, numbers, and short horizontal lines (the rest are not).
To be sure, the first character of the intercepted string must be /;
And there must be a bunch of English between them.
Ordinary single interception, multiple interception, or by intercepting two / between characters and replacing the original string.
howeverWhat’s the best performance?
Adopt
Six Available Answers: Performance Test Results—— Chrome 53
Border Town: about 5,790,000 executions per second;
Xzavier: about 5,390,000 executions per second;
Questioning the Central Plains: about 4,310,000 executions per second;
Agree and accept: about 6,590,000 executions per second;
Hping: about 4,730,000 executions per second;
A fool is not foolish: about 2,790,000 executions per second;
Consensus and acceptance.split('/')[2]The method has the best performance.

7 Answers
Best Answer
Agree and accept answered 1 week ago

Split is OK, the simplest is it.~

'/test/gagaga'.split('/')[2]
"gagaga"
'/hahaha/yr-1-k'.split('/')[2]
"yr-1-k"
xzavier answered 1 week ago

We can confirm that the first character of the intercepted string must be /; and the first character between the two must be a string of English, anyway, we need to use the string method, with fewer steps, fewer code, and the corresponding performance will be good.
As you said:

'/test/gagaga'.replace(/\/.+\//,'');  //"gagaga"
'/hahaha/yr-1-k'.replace(/\/.+\//,''); //"yr-1-k"
xiaoboost answered 1 week ago
  1. Zero Width Assertion Back Matching
  2. execMethod, Partial Matching
xzavier replied 1 week ago

http://www.cnblogs.com/creek/…But JS does not support zero width forward review assertion

Border town answered 1 week ago
var s = "/test/gagaga"
var r = (s.match(/^\/.*?\/(.*)$/) || [])[1]
Questioning the Central Plains answered 1 week ago
/* Alphabet, Number, Short Horizontal Line? * /
    /\/([a-zA-Z0-9\-]+)$/g