Doubt replace javascript function

Asked

Viewed 124 times

0

Anyone 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;


}

1 answer

0


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

/</g

Its purpose is to identify the effects of < and the g specifies that the query must be performed in the entire string

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

http:\/\/\S+

Its purpose is to identify the incidences of words beginning with http:// and the 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 is worth noting that all these regex instructions are poorly written and little restricted, summarizing would probably replace a lot of unwanted things

  • Reading tip : http://answall.com/questions/110701/o-que-significa-o-shortcut-s-nas-regex

Browser other questions tagged

You are not signed in. Login or sign up in order to post.