I got HTML element contains this:
P6C245RO
For the string
" this is my string "
You probably want to make the excessive spaces single spaces, but remove the leading and trailing spaces entirely. For that, add another .replace
s.replace(/\s+/g, " ").replace(/^\s|\s$/g, "");
For
"this is my string"
Update
s.replace(/\s+/g, " ").trim()
Thanks, @Pier-Luc Gendreau