JQuery Trim to remove the string before and after the empty character implementation method

  • 2020-03-30 01:41:15
  • OfStack

If your project is using the jQuery framework, to remove the empty characters at the beginning and end of a string you would of course choose: jquery.trim (string). How to implement it without using jQuery? I've previously sent a snippet of code: Javascript to remove the left and right whitespace-trim () from the string, which is rough and requires a recursive operation. So a lot of people are unhappy with the quality of the code, including me.
Once in a while, I looked at the code of jQuery and found it very worthwhile for us to learn.

To see how it does this, the code has one sentence: use the regular method.

JavaScript Trim implementation code
The function trim (text) {
Replace (/^/s+|/s+$/g, "");
}

If you don't use jQuery or other frameworks in your project, you can Copy this code to use this feature. Trust the structure and quality of the code to your satisfaction.


Related articles: