简体   繁体   中英

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. 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. 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. Because; if you know it's there, why do you need to find it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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