Reputation: 7267
Here I am using JavaScript to find the previous line number of the given occurrence.
Here I have this text
iriwr a
!!!!!!!!!
jaljflsjfa
fasjfkjaslf
!!!
ldjadada
!!!!!!!!
mlakifkqwoieqkwe
dalkdajsdja
!!!!!!!!!
In this text I have the ! character, now I have to get the previous line numbers where 5 or more exclamation marks are present.
For example:
On line 5 (starting from 1) there are only 3 !. That should not count. The code should count only if there are more than 5! then find out the previous line numbers.
i.e in line number 2, 7, 10 there are more than 5 !s so it should give previous line numbers as 1,6,9.
How can I do this?
This is what I have tried.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css" type="text/css">
<script>
function getnum() {
var str = document.getElementById('strin').innerHTML;
var n = str.search("!!!");
document.getElementById("demo").innerHTML = n;
}
</script>
<body>
<textarea rows="12" cols="15" id="strin">
iriwr a
!!!!!!!!!
jaljflsjfa
fasjfkjaslf
!!!!!!!!
ldjadada
!!!!!!!!
!!!!!!
mlakifkqwoieqkwe
dalkdajsdja
!!!!!!!!!
</textarea>
<button onclick="getnum()">Get line number</button>
<p id="demo"></p>
</body>
</html>
Upvotes: 0
Views: 67
Reputation: 4868
function getnum() {
var str = document.getElementById('strin').innerHTML;
var arr = str.split("\n");
var lines="";
for(i=0;i<arr.length;++i){
if(arr[i].search("!!!!!")!=-1){
lines+=" "+i;
}
}
document.getElementById("demo").innerHTML = lines;
}
Try this function. It will return line number sepearted by space, but you can format in whatever way you like.
Upvotes: 1