这些JS速记字符是什么意思? [重复]

时间:2022-10-06 12:18:53

This question already has an answer here:

这个问题在这里已有答案:

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.

我理解其中一些所以我有以下问题。

  1. what does the "/^" do?
  2. “/ ^”有什么作用?

  3. what does the ? do?
  4. 是什么?做?

  5. what do the "(" and ")" do around the https
  6. https周围的“(”和“)”做了什么

  7. what does the ":" do?
  8. “:”是做什么的?

  9. what does the "i" do?
  10. “我”做什么?

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

  • Starting point: RegexOne
  • 起点:RegexOne

  • Testing online with RegExp Explanation: RegEx101
  • 使用RegExp在线测试说明:RegEx101

#2


1  

Might this help you

这可能对你有所帮助

  • ^ assert position at start of the string
  • ^断言字符串开头的位置

  • http matches the characters http literally (case insensitive)
  • http匹配字符http字面意思(不区分大小写)

  • s? matches the character s literally (case insensitive)
  • S'从字面上匹配字符(不区分大小写)

  • 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])
  • 我修饰语:不敏感。不区分大小写的匹配(忽略[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

  • Starting point: RegexOne
  • 起点:RegexOne

  • Testing online with RegExp Explanation: RegEx101
  • 使用RegExp在线测试说明:RegEx101

#2


1  

Might this help you

这可能对你有所帮助

  • ^ assert position at start of the string
  • ^断言字符串开头的位置

  • http matches the characters http literally (case insensitive)
  • http匹配字符http字面意思(不区分大小写)

  • s? matches the character s literally (case insensitive)
  • S'从字面上匹配字符(不区分大小写)

  • 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])
  • 我修饰语:不敏感。不区分大小写的匹配(忽略[a-zA-Z]的情况)