检查字符串是否包含换行符 [英] check whether string contains a line break

查看:299
本文介绍了检查字符串是否包含换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我必须得到textarea的HTML并检查它是否包含换行符。如何查看它是否包含 \ n ,因为使用 val()返回的字符串不包含 \ n 我无法检测到它。我尝试使用 .split(\ n),但它给出了相同的结果。怎么办呢?

So, I have to get HTML of textarea and check whether it contains line break. How can i see whether it contain \n because the string return using val() does not contain \n and i am not able to detect it. I tried using .split("\n") but it gave the same result. How can it be done ?

一分钟,IDK为什么我把 \ n 添加到textarea作为值,它会中断并转移到下一行。

One minute, IDK why when i add \n to textarea as value, it breaks and move to next line.

推荐答案

HTML中的换行符不由 \表示n \ r 。它们可以通过多种方式表示,包括< br> 元素,或其他任何块元素(< p>< / p>< p>< / p> ,例如)。

Line breaks in HTML aren't represented by \n or \r. They can be represented in lots of ways, including the <br> element, or any block element following another (<p></p><p></p>, for instance).

如果您使用的是 textarea ,你可能找到 \ n \ r (或 \\\ )换行,所以:

If you're using a textarea, you may find \n or \r (or \r\n) for line breaks, so:

var text = $("#theTextArea").val();
var match = /\r|\n/.exec(text);
if (match) {
    // Found one, look at `match` for details, in particular `match.index`
}

实例 来源

......但那是只是 textarea s,而不是一般的HTML元素。

...but that's just textareas, not HTML elements in general.

这篇关于检查字符串是否包含换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆