I have the following text:
var text=
"The sad sad man uses a bat to swing the bats
away from his sad garden .
Sadly he doesn't succeed. "
Let’s say i want to search for the word "sad".
var match;
re = /sad/g,
match;
while (match = re.exec(text)) {
console.log(match);
match.poz = ....
}
How can i make match.poz to be a tuple(array) like this [line,position on the collumn] all starting from 0,0 ?
Eg.
- 1 match –> match.poz = [0,4]
- 2 match –> match.poz = [0,8]
- 3 match –> match.poz = [1,14]
- 4 match –> match.poz = [2,0]
I was able to build a simple parser, instead of using a regex, which I don’t think is possible (without a lot of help) to get the position in Javascript. All this does is go through the line, one character at a time, and “peek” ahead to see if the current position gives either
sador\n.Which gives me the following in Firebug console:
http://jsfiddle.net/Zx5CK/1/