Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

javascript - check whether string contains a line break

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

Live Example | Source

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...