jQuery 如何使用 node.js 修剪字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30778399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trim the string using node.js?
提问by Vanarajan
I am trying to trim the string. I google it for trim.
我正在尝试修剪字符串。我谷歌它修剪。
The tips i am getting.
我得到的提示。
- name.trim(); -- Not recognized trim() function
npm install --save string
var S = require('string'); S('hello ').trim().s; -- this also same issue
- 名称.修剪(); -- 无法识别 trim() 函数
npm install --save 字符串
var S = require('string'); S('hello ').trim().s; -- this also same issue
Can you any one assist me what is the problem of the above method or anything i have missed it?
您可以帮助我上述方法有什么问题或我错过了什么吗?
回答by Alex
You don't need jquery for this...
你不需要jquery...
var str = " Hello World! ";
var trimmedStr = str.trim();
console.log(trimmedStr);
This will output the following in the console:
这将在控制台中输出以下内容:
"Hello World!"
“你好,世界!”
回答by CinchBlue
You don't need to convert anything. From the Node.js console:
你不需要转换任何东西。从 Node.js 控制台:
' hello '.trim()
works fine. It reduces to:
工作正常。它简化为:
'hello'
as expected. You need only invoke the function on the string literal or a string variable.
正如预期的那样。您只需要在字符串文字或字符串变量上调用该函数。
回答by Luxknight007
Try this work 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);