REGEX从div中选择前两行

时间:2013-09-11 04:51:59

标签: javascript regex

有没有办法从div中的文本中选择前两行。

Content in div:

/*------------------------
| some content goes here |
| inside div or any      |
| container in HTML. is  |
| it possible to select  |
| first two lines        |
| displaying in browser  |
----------------------- */

SelectedText = "some content goes here inside div or any"

2 个答案:

答案 0 :(得分:0)

var first_line = $("#divElement").text().split("\n")[1];
var second_line = $("#divElement").text().split("\n")[2];

JSFiddle Example

答案 1 :(得分:0)

您好,您可以查看以下代码

 <script type="text/javascript">
function runTest(){
var str=document.getElementById("id1").value;
var arr = str.split(/[\n\r]/g);
alert(arr[0]+arr[1]);
}
</script>
<textarea id="id1">
some content goes here
inside div or any
container in HTML. is 
it possible to select
first two lines 
displaying in browser
</textarea>

<a href="#" onClick="runTest();">myLink</a>