Here is a test:
console.log(" Golden Katana of the Unflinching Dawn ".replace(/\s+/g,""))
I want to remove the extra spaces at the end of the string but it removes every space in the string, so how could I remove just the extra spaces at the end and just keep Golden Katana of the Unflinching Dawn
?
JavaScript String trim() The trim() method removes whitespace from both sides of a string.
Use JavaScript's string. replace() method with a regular expression to remove extra spaces. The dedicated RegEx to match any whitespace character is \s . Expand the whitespace selection from a single space to multiple using the \s+ RegEx.
The Trim function The most obvious (and generally efficient) method for removing both leading and trailing space is to use the TRIM() function.
You can use str.trim()
for this case. It removes the leading and trailing whitespace from a string. The regular expression str.replace(/^\s+|\s+$/g,'')
will alternatively do the same thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With