gpt4 book ai didi

javascript - 如何计算 javascript 中字符串末尾的换行符数量?

转载 作者:行者123 更新时间:2023-11-30 13:38:48 25 4
gpt4 key购买 nike

例如我有一个如下所示的字符串:

var example_string = "this is the example string\n <-don't want \n\n\n";

这里我创建了一个字符串,其中有四个换行符,最后是三个连续的换行符。我如何以编程方式让 javascript 告诉我末尾有 3 个换行符?

最佳答案

example_string.match(/\n*$/)[0].length

(\n*$ = 字符串末尾前有 0 个或多个换行符,.match(...)[0] = 整个字符串匹配)

关于javascript - 如何计算 javascript 中字符串末尾的换行符数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3446250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com