-1

I have a very simple code that converts every letter to lower case and later sets upper case for every first letter using RegExp:

let quote = 'I dO nOT lIke gREen eGgS anD HAM';
let fixQuote = quote.toLocaleLowerCase('en-US');

let regex = /(^\w|\s\w)/g;

let fixedQuote = fixQuote.replace(regex, m => m.toUpperCase());

Can someone explain what does the arrow function m => m. does in this part? I don't understand what m stands for here.

Thanks!

3
  • 1
    It replaces every match with its uppercase equivalent. Commented Jul 18, 2020 at 16:56
  • It is explained in the String#replace() docs Commented Jul 18, 2020 at 16:57
  • it's like this function f(m){return m.toUpperCase();} Commented Jul 18, 2020 at 16:58

2 Answers 2

3

It is simply the internal variable of an anonymous function:

  • m is the parameter variable with value of regex.
  • m.toUpperCase() is the return value.

Essentially the same as:

function toUpperCase(m){
    return m.toUpperCase();
}
Sign up to request clarification or add additional context in comments.

Comments

2

In this case m is just a parameter to a function, so a variable name. You can imagine that function written like this:

function toUpperCase(m) {
   return m.toUpperCase();
}

Every match found by your regex will be passed to the function as an argument. The variable m will hold that value.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.