Duvida replace function javascript

0

Someone can explain to me what these parameters mean within the function replace (/</g) , (http:\/\/\S+)/g

function escape(s) {


  var text = s.replace(/</g, '&lt;').replace('"', '&quot;');


  // URLs
  text = text.replace(/(http:\/\/\S+)/g, '<a href="$1">$1</a>');


  // [[img123|Description]]


  text = text.replace(/\[\[(\w+)\|(.+?)\]\]/g, '<img alt="$2" src="$1.gif">');


  return text;


}
    
asked by anonymous 14.02.2016 / 01:58

1 answer

0

Both are regex statements, considering the code you provided yourself are being used to replace the unwanted html content of certain strings.

/</g

Its purpose is to identify the occurrences of the < character and the g specifies that the query must be done in the entire string

Considering the application in your code it is replacing the incidents of '<' by its equivalence in ASCII code '<' in general this is to prevent browsers from understanding the '<' as html code.

http:\/\/\S+

It is intended to identify occurrences of words beginning with http:// and S+ informs that http: // can be followed by any character other than a space.

Considering the application of your code this would aim to remove the incidences of urls from the string.

It's worth mentioning that all of these regex statements are poorly written and unrestricted, so summarizing would probably replace a lot of unwanted

    
14.02.2016 / 02:24