How to trim a string using node.js?

I am trying to trim a string. I do this for cropping.

The tips that I get.

  • name.trim (); - Unrecognized trim () function
  • npm install --save string

    var S = require('string'); S('hello ').trim().s; -- this also same issue 

Can you help me with the problem of the above method or something that I missed?

+5
source share
3 answers

You don't need jquery for this ...

 var str = " Hello World! "; var trimmedStr = str.trim(); console.log(trimmedStr); 

The following will be displayed in the console:

"Hello World!"

+10
source

You do not need to convert anything. On the Node.js console:

 ' hello '.trim() 

works great. It comes down to:

 'hello' 

as was expected. You only need to call the function in a string literal or string variable.

0
source

Try this job for me

 var str = " Hello World! "; var trim_leftright = str.replace(/^\s*|\s*$/g, ''); // left and right trim console.log(trimleftright); var trim_left = str.replace(/^\s*/, ''); // left trim console.log(trim_left); var trim_right = str.replace(/\s*$/, ''); // right trim console.log(trim_right); 
0
source

All Articles