Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do these JS shorthand characters mean? [duplicate]

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. what does the ? do?
  3. what do the "(" and ")" do around the https
  4. what does the ":" do?
  5. what does the "i" do?

If this appears to be a question without research, any guidance where to start would be great.

like image 599
P0lska Avatar asked May 28 '15 08:05

P0lska


People also ask

What this regex means?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.

Which is the correct shorthand regex?

I prefer "re", but it depends on the language. JavaScript lists it as "RegExp", whereas most other languages just call it "RegEx."

What are regex patterns?

A regular expression is a pattern that the regular expression engine attempts to match in input text. A pattern consists of one or more character literals, operators, or constructs.


2 Answers

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://

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
  • Testing online with RegExp Explanation: RegEx101
like image 152
SharpEdge Avatar answered Sep 25 '22 15:09

SharpEdge


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])
like image 44
Satpal Avatar answered Sep 24 '22 15:09

Satpal