string.replace(/^\s+|\s+$/g, "");
javascript regex white spaces before and after "sentence" need to be removed
-
04-07-2022 - |
سؤال
I need to remove all the white spaces before the first character (which is not a white space) and remove all the white spaces after the last character (which is not a white space).
Looks like this:
' a boat has an anchor '
Should be 'a boat has an anchor'
' $$%&@$&$%& lollerskates #372734§ '
Should be '$$%&@$&$%& lollerskates #372734§'
Please ignore the single quotes, they are there to show the white space.
المحلول
نصائح أخرى
There is a native method for this. string.trim
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
var orig = " foo ";
alert( orig.trim() );
لا تنتمي إلى StackOverflow