Remove whitespace from start and end (Regex)

/^\s+|\s+$/g
I am getting bit confused here, could someone explain why do I need to put the or in between ,shouldn’t the + look for multiple spaces and it is supposed to work without using |?

Not if it is only the start and end spaces you have to match on.

' This has spaces '.match(/\s+/g)
// [' ', ' ', ' ', ' ']

' This has spaces '.match(/^\s+|\s+$/g)
// [' ', ' ']

Thank you so much. Much clearer now .

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.