简体   繁体   English

使用Javascript在Textarea中查找文本行

[英]Find Line of Text in Textarea With Javascript

I saw some code a couple of days ago but I can't find it again. 几天前我看到了一些代码,但是我再也找不到了。 I have a textarea and there is [source] word inside the textarea. 我有一个textarea,并且在textarea中有[source]字。 I want to find this word's line. 我想找到这个单词的行。

Code was like this: 代码是这样的:

document.getElementById('').value.match(/[source]/i).length

However I miss the code and I can't find it again. 但是我错过了代码,无法再次找到它。 I don't want to do with loop. 我不想做循环。 It was really easy code. 这真的很简单。

This? 这个?

   content = "foo\n bar [source] baz\n quux"
   content.match(/.*\[source\].*/)[0]
   // " bar [source] baz"

To find a line number (without loops) 查找行号(无循环)

 line = content.match(/[\s\S]*(?=\[source\])/)[0].replace(/[^\n]/g, "").length

Although a loop would be definitely more readable. 尽管循环绝对会更易读。

Well, first of all; 好吧,首先; you spelled "Source" wrong in your example. 您在示例中将“ Source”拼写错误。 When searching for exact matches, you want to get the spelling correctly. 搜索完全匹配时,您希望正确获得拼写。

Also; 也; it would help a lot if you told us in what context you would like to find "[source]" inside a textarea. 如果您告诉我们您想在哪种文本区域内找到“ [source]”,这将大有帮助。 Because; 因为; if you know it's there, why do you need to find it? 如果您知道它在那里,为什么需要找到它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM