Javascript 正则表达式匹配 textarea 中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7481099/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:27:25 来源:igfitidea点击:
Regex match newline in textarea
提问by OptimusCrime
I have to get the value from a textarea using jQuery and count the number of newlines there. I'd like to do this using a regex-expression. Does anyone know how to do that?
我必须使用 jQuery 从 textarea 获取值并计算那里的换行数。我想使用正则表达式来做到这一点。有谁知道这是怎么做到的吗?
回答by i100
regex does not have count. better use array like this
正则表达式没有计数。更好地使用这样的数组
var val = textarea.value;
var arr = val.split(/[\n\r]/g);
var count = arr.length;
you could condense this in less rows and vars...
您可以将其压缩为更少的行和变量...
var count = $('textarea').val().split(/[\n\r]/g).length;
回答by Carlos Gant
$(document).ready(function() {
var str = $("#txtField").val();
parts = str.split(/[\n\r]/g);
var newline_count = parts.length;
alert("Count: " + newline_count);
});