This question already has an answer here:
这个问题在这里已有答案:
- Reference - What does this regex mean? 1 answer
参考 - 这个正则表达式意味着什么? 1个答案
I've been trying to find out what this code means but I haven't had any luck even finding out where to start or what to look up.
我一直试图找出这个代码意味着什么,但我还没有运气,甚至找不到从哪里开始或查找什么。
if(!/^(https?):\/\//i.test(value))
I understand some of it so I've got the following questions.
我理解其中一些所以我有以下问题。
- what does the "/^" do?
- what does the ? do?
- what do the "(" and ")" do around the https
- what does the ":" do?
- what does the "i" do?
“/ ^”有什么作用?
是什么?做?
https周围的“(”和“)”做了什么
“:”是做什么的?
“我”做什么?
If this appears to be a question without research, any guidance where to start would be great.
如果这似乎是一个没有研究的问题,任何从哪里开始的指导都会很棒。
2 个解决方案
#1
7
What is it
A regular expression is an object that describes a pattern of characters.
正则表达式是描述字符模式的对象。
Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text.
正则表达式用于在文本上执行模式匹配和“搜索和替换”功能。
This is exactly the same but maybe more clear
这完全相同,但可能更清楚
var patt = /^(https?):\/\//i;
if( !patt.test(value) ){
// value DOES NOT MATCH patt!
}
What it does
In this case it checks that value
doesn't start with http://
or https://
在这种情况下,它会检查该值是否以http://或https://开头
RegExp Explanation
/ //Open regexp
^ //Start of the string
( // Start of the capturing group
https? //Match literally http or https (because s is optional "?")
) // End of capturing group
:\/\/ // Match literally ://
/ // Close regexp
i // Case-insensitive flag
Learning
#2
1
Might this help you
这可能对你有所帮助
-
^
assert position at start of the string -
http
matches the characters http literally (case insensitive) -
s?
matches the character s literally (case insensitive) - Quantifier:
?
Between zero and one time, as many times as possible, giving back as needed [greedy] -
:
matches the character : literally -
\/
matches the character / literally -
i
modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
^断言字符串开头的位置
http匹配字符http字面意思(不区分大小写)
S'从字面上匹配字符(不区分大小写)
量词:?在零和一次之间,尽可能多次,根据需要回馈[贪心]
:匹配字符:字面意思
\ /匹配字符/字面
我修饰语:不敏感。不区分大小写的匹配(忽略[a-zA-Z]的情况)
#1
7
What is it
A regular expression is an object that describes a pattern of characters.
正则表达式是描述字符模式的对象。
Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text.
正则表达式用于在文本上执行模式匹配和“搜索和替换”功能。
This is exactly the same but maybe more clear
这完全相同,但可能更清楚
var patt = /^(https?):\/\//i;
if( !patt.test(value) ){
// value DOES NOT MATCH patt!
}
What it does
In this case it checks that value
doesn't start with http://
or https://
在这种情况下,它会检查该值是否以http://或https://开头
RegExp Explanation
/ //Open regexp
^ //Start of the string
( // Start of the capturing group
https? //Match literally http or https (because s is optional "?")
) // End of capturing group
:\/\/ // Match literally ://
/ // Close regexp
i // Case-insensitive flag
Learning
#2
1
Might this help you
这可能对你有所帮助
-
^
assert position at start of the string -
http
matches the characters http literally (case insensitive) -
s?
matches the character s literally (case insensitive) - Quantifier:
?
Between zero and one time, as many times as possible, giving back as needed [greedy] -
:
matches the character : literally -
\/
matches the character / literally -
i
modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
^断言字符串开头的位置
http匹配字符http字面意思(不区分大小写)
S'从字面上匹配字符(不区分大小写)
量词:?在零和一次之间,尽可能多次,根据需要回馈[贪心]
:匹配字符:字面意思
\ /匹配字符/字面
我修饰语:不敏感。不区分大小写的匹配(忽略[a-zA-Z]的情况)