regex - JavaScript - Understanding the role of a variable in a function passed as an argument -


i'm trying work regular expressions, in particular i've shuffle middle content of string. found example fits needs here , answer i'm studying 1 brian nickel.

this code proposed brian nickel in question:

mystr.replace(/\b([a-z])([a-z]+)([a-z])\b/ig, function(str, first, middle, last) { return first +        middle.split('').sort(function(){return math.random()-0.5}).join('') +         last; }); 

i'm beginner in javascript , regex, see here function passed argument, don't understand why there 4 parameters, in particular not understand first parameter str , why if remove it, function doesn't work anymore correctly.

i it's silly question, don't found want on web, or maybe don't know how search properly. in advance

when using replace regexp, function use callback receive 1+n parameters n match inside parenthesis.

the come in order :

  1. the complete matched string.
  2. the first parenthesis.
  3. the second parenthesis.
  4. go on...

if remove str , argument first become matched string. if don't use argument, need it!


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -