直接截断字符串 JavaScript
我想使用直接 JavaScript 截断动态加载的字符串。这是一个网址,所以没有空格,而且我显然不关心单词边界,只关心字符。
这是我得到的:
var pathname = document.referrer; //wont work if accessing file:// paths
document.getElementById("foo").innerHTML = "<a href='" + pathname +"'>" + pathname +"</a>"
I'd like to truncate a dynamically loaded string using straight JavaScript. It's a url, so there are no spaces, and I obviously don't care about word boundaries, just characters.
Here's what I got:
var pathname = document.referrer; //wont work if accessing file:// paths
document.getElementById("foo").innerHTML = "<a href='" + pathname +"'>" + pathname +"</a>"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
使用 substring 方法:
所以在你的情况下:
Use the substring method:
So in your case:
这是您可以使用的一种方法。这是 FreeCodeCamp 挑战之一的答案:
Here's one method you can use. This is the answer for one of FreeCodeCamp Challenges:
更新了ES6版本
Updated ES6 version
是的,子串。你不需要做 Math.min;索引比字符串长度长的子字符串以原始长度结束。
但!
这是一个错误。如果 document.referrer 中有撇号怎么办?或者在 HTML 中具有特殊含义的各种其他字符。在最坏的情况下,引用者中的攻击者代码可能会将 JavaScript 注入您的页面,这是一个 XSS 安全漏洞。
虽然可以手动转义路径名中的字符来阻止这种情况发生,但这有点痛苦。使用 DOM 方法比摆弄 insideHTML 字符串更好。
yes, substring. You don't need to do a Math.min; substring with a longer index than the length of the string ends at the original length.
But!
This is a mistake. What if document.referrer had an apostrophe in? Or various other characters that have special meaning in HTML. In the worst case, attacker code in the referrer could inject JavaScript into your page, which is a XSS security hole.
Whilst it's possible to escape the characters in pathname manually to stop this happening, it's a bit of a pain. You're better off using DOM methods than fiddling with innerHTML strings.
以下代码截断字符串并且不会拆分单词,而是丢弃发生截断的单词。完全基于 Sugar.js 源。
Following code truncates a string and will not split words up, and instead discard the word where the truncation occurred. Totally based on Sugar.js source.
我想我会提到 Sugar.js 。它有一个非常聪明的截断方法。
来自文档:
示例:
输出:
Thought I would give Sugar.js a mention. It has a truncate method that is pretty smart.
From the documentation:
Example:
Output:
一条线ES6解决方案
One line ES6 Solution
是的,
substring
效果很好:Yes,
substring
works great:要将字符串截断为特定长度,请在 JavaScript 中使用以下单线性箭头函数:
上面的函数使用
String.prototype.slice
方法,该方法获取字符串的一大块并将其返回为一个新的字符串而不改变原来的字符串。For truncating a String to a specific length, use the following one-linear arrow function in JavaScript:
The above function uses the
String.prototype.slice
method, which takes a chunk of a string and returns it as a new string without changing the original.除了 substring 方法之外,我还为此找到了一个不错的小 JS 库。
它在普通 javascript 中有多种有用的方法来截断字符串。
按字符截断:
只需将
class
添加到a
并初始化插件即可。多行文本剪辑也是可能的。
github 上的 cuttr.js 文档 中提到了更多选项,例如单词或句子截断页。
Besides the substring method, i found a nice little JS lib for this purpose.
It has mutliple useful methods in vanilla javascript to truncate a string.
Truncation by characters:
Simply add a
class
to thea
and init the plugin.Multiline text clipping is also possible.
More options like word oder sentences truncation are mentioned in the cuttr.js docs on the github page.
您可以借助内部 JavaScript 方法修复此方法
You can fix this method with the help of internal JavaScript methods
如果你想按单词截断。
in case you want to truncate by word.
逻辑如下
获取字符串的长度
如果超过阈值,则获取
子字符串并以省略号或其他符号结尾
否则,返回输入字符串
函数 truncateString(str: 字符串, num: 数字, 结尾: 字符串 = '...') {
返回 str.length >号? str.slice(0, num) + 结尾: str;
}
The logic will be as follows
get the length of the string
if it is more than a threshold, get the
substring and end with ellipsis or other notation
else, return the input string
function truncateString(str: string, num: number, ending: string = '...') {
return str.length > num ? str.slice(0, num) + ending : str;
}
如果您想按 Limit(符号)截断,
但您不想剪切非长文本(例如帖子标题)的单词(保留最后一个单词完整):
对于长字符串(帖子的一些长文本 - Vue-3 用作过滤器):
In case you want to truncate by Limit (symbols),
but you don't want to cut the words (leave the last word intact) for not LONG text (head of post for example):
For long String (some long Text of Post - Vue-3 use as Filter):