Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有办法限制字符串?例子:
{{item | limit(50)}}
或者可能是子字符串函数?
没有一个开箱即用的过滤器可以做到这一点,但您可以使用替换来做到这一点。
{{ item | replace("^(.{50,50})(.*)", "$1") }}
这将创建一个正则表达式,它捕获前 50 个字符(任何字符:'.' 在 50 到 50 次 '{50,50}' 之间),然后将其放入 $1 中,剩下的任何内容都保留为 $2。然后你只用 1 美元替换结果......基本上扔掉了 2 美元。这看起来有点奇怪,但它会完成这项工作。