18

I came across this JavaScript function:

function myTrim(x) {
  return x.replace(/^\s+|\s+$/gm,'');
}

I know that this function(mytrim()) replaces some characters in the string(x), but what does /^\s+|\s+$/gm do in the replace method?

Where can I learn more about these things?

Note- This function returns the string with removed spaces at both sides.

pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
aryaman
  • 446
  • 2
  • 7
  • 15
  • 1
    While JavaScript is a language, it is very commonly—but not exclusively—used in the context of the HTML DOM in a web browser. That makes web documentation a great place for learning JavaScript in general. The major web browser vendors have [centralized](https://blog.mozilla.org/blog/2017/10/18/mozilla-brings-microsoft-google-w3c-samsung-together-create-cross-browser-documentation-mdn/) their general documentation efforts at [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/). The keywords, in this case, are "replace" and "RegExp". – Tom Blodget Mar 10 '18 at 18:23
  • @aryaman, have you found a suitable solution in any of those # answers? If not, tell us more. Otherwise do and accept the solution that is best for you to close the question - see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – ino Mar 11 '18 at 07:48
  • Does this answer your question? [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Vega Jun 26 '21 at 05:51

4 Answers4

24

It is a regular expression search that matches two alternative patterns:

/^\s+|\s+$/gm

/ Regex separator

First Alternative ^\s+

  • ^ asserts position at start of a line
  • \s+ matches any whitespace character (equal to [\r\n\t\f\v ])
  • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

Second Alternative \s+$

  • \s+ matches any whitespace character (equal to [\r\n\t\f\v ])
  • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
  • $ asserts position at the end of a line

Global pattern flags

  • g modifier: global. All matches (don't return after first match)
  • m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

You can read more details on regex101.com.

Function explanation

The function call return x.replace(/^\s+|\s+$/gm,''); searches for any spaces from the beginning of the string and from the end of string. If found then it is replaced by empty string ''. Simply said it does the trim whitespace characters:

  • \n carriage return (ASCII 13)
  • \r line-feed (newline) character (ASCII 10)
  • \t tab character (ASCII 9)
  • \f form-feed character (ASCII 12)
  • \v any vertical whitespace character
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ino
  • 2,345
  • 1
  • 15
  • 27
2

This syntax is called a regular expression (often shortened to RegEx); there are multiple places you can learn this, but you can try this one.

There are also multiple websites to test such regular expressions such as regex101.com. Note that regular expressions are not a universal standard; there are variants depending on the programming language and platform (for example, grep, extended grep, Perl, Java, etc.).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simon Berthiaume
  • 643
  • 4
  • 11
2

It is a regular expression.

That pattern replaces all whitespace characters \s+ by an empty string depending on that is is at the beginning of string ^\s+ or | at the end of the string \s+$.

g is for global modifier, what doesn't return after first match.

m is for multiline.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
standby954
  • 209
  • 1
  • 7
2

^\s+|\s+$gm

1st Alternative (^\s+)

^ asserts position at start of a line

\s+ matches any whitespace character (equal to [\r\n\t\f\v ])

+Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

2nd Alternative (\s+$)

\s+ matches any whitespace character (equal to [\r\n\t\f\v ])

+Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

$ asserts position at the end of a line

Global pattern flags:

g modifier: global. All matches (don't return after first match)

m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Community
  • 1
  • 1
Anjali Sharma
  • 535
  • 6
  • 15