lua 字符串 trim_使用trim()调整JavaScript字符串

lua 字符串 trim

User-submitted text entered into forms is often padded with spaces, usually generated by accidentally hitting the space bar inside a text input. Site owners almost always want to erase this extra data: it would be confusing for a user to have their login attempt rejected just because their original signup included an unseen space.

输入到表单中的用户提交的文本通常会填充空格,通常是由于意外击中文本输入内部的空格键而产生的。 站点所有者几乎总是希望删除这些额外的数据:仅因为其原始注册中包含看不见的空间,否则用户拒绝登录尝试会造成混乱。

Removing spaces - which covers spaces, tab, no-break spaces, carriage returns and line feed characters - is the function of the trim() method. Given a variable username:

trim()方法的功能是删除空格(包括空格,制表符,不间断空格,回车符和换行符trim() 。 给定变量 username

var username = "dstorey ";

Trimming it in the console will create:

控制台中对其进行修剪将创建:

console.log(username.trim());
> "dstorey"

Note that spaces inside a string are preserved after a trim(); the method only affects whitespaces at the start and end of a string.

注意,字符串中的空格 trim()之后保留; 该方法仅影响字符串开头和结尾的空格。

More frequently, the result of a trim() is set to a variable. Given a text input with an id and name of username:

更常见的是, trim()的结果设置为变量。 给定一个文本inputidnameusername

var username = document.getElementById("username").value.trim();

Developers often use the term “trim” when referring to removing characters in general: for example “trimming off a file extension” or “trimming the first character”. Technically, trim() is only relevant to removing spaces, and all other actions will use other methods (replace() and slice respectively, in the examples provided.)

开发人员通常在泛指删除字符时使用术语“修剪”:例如,“修剪文件扩展名”或“修剪第一个字符”。 从技术上讲, trim() 与删除空格有关,所有其他操作将使用其他方法(在提供的示例中分别为replace()slice )。

支持与替代 (Support & Alternatives)

trim() is functionally equivalent to JQuery’s $.trim(str); the native method is supported in all modern browsers, including IE9+ and Safari 5, so it’s advisable to use the native trim() unless historical browser support is required.

trim()在功能上等效于JQuery的$.trim(str) ; 包括IE9 +和Safari 5在内的所有现代浏览器均支持该本机方法,因此,除非需要历史浏览器支持,否则建议使用本机trim()

翻译自: https://thenewcode.com/1099/Tailoring-JavaScript-Strings-with-trim

lua 字符串 trim

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值