match regular match
var e = "www.apple.com:@baidu.com"
var match = e.match(/com/g)
console.log("match: "+match);
> "match: com,com"
match return value problem
The return value of match is an array.
The 0th element of the array is the result of matching the entire regular expression. The
1st element of the array is the matching result with the first subexpression (that is, the first parenthesis of the regular expression. part)
The second element of the array is the content corresponding to the second subexpression (that is, the part inside the second bracket) in the matching result, and so
on
Sample code:
var e = "https://www.apple.com:@baidu.com"
var match = e.match(/^https?\:\/\/([^\/:?#]+)(?:[\/:?#]|$)/)
console.log("match: "+match);
console.log("match[0]: "+match[0]);
console.log("match[1]: "+match[1]);
console.log("match[2]: "+match[2]);
Regular expressions: /^https?\:\/\/([^\/:?#]+)(?:[\/:?#]|$)/
https://jex.im/regulex/
output:
> "match: https://www.apple.com:,www.apple.com"
> "match[0]: https://www.apple.com:"
> "match[1]: www.apple.com"
> "match[2]: undefined"
Among them:
match[0] is the result of "https://www.apple.com:@baidu.com"
the entire match: match[1] is the part corresponding to the first parenthesis in and match[2] is the corresponding part in the second parenthesis : undefined/^https?\:\/\/([^\/:?#]+)(?:[\/:?#]|$)/
https://www.apple.com:
https://www.apple.com:
([^\/:?#]+)
www.apple.com
https://www.apple.com:
(?:[\/:?#]|$)
another example
var e = "abcdx.efghx"
var match = e.match(/efg(.x)/)
console.log("match: "+match);
console.log("match[0]: "+match[0]);
console.log("match[1]: "+match[1]);
console.log("match[2]: "+match[2]);
output:
> "match: efghx,hx"
> "match[0]: efghx"
> "match[1]: hx"
> "match[2]: undefined"
Reference:
https://www.w3school.com.cn/jsref/jsref_match.asp
https://blog.csdn.net/weixin_43791776/article/details/84455293