I Have this example : https://jsfiddle.net/xqdwL914/1/
<div id="test">bar <i>def</i>ghij<br>bar <i>def</i>ghij</div>
I Want to find "bar" index and length in multi occurrence like this :
var node = document.getElementById('test');
var text = node.textContent;
var re = /bar/g;
while ((match = re.exec(text)) != null) {
alert("match found at: " + match.index+ " length: " +match.length);
}
the output :
match found at: 0 length: 1
match found at: 11 length: 1
Why the length is "1" it should be "3" as three character of the word "bar" and how i get the last index of each match word bar ????
From MDN
If the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item
var text = 'bar <i>def</i>ghij<br>bar <i>def</i>ghij';
var re = /bar/g;
while ((match = re.exec(text)) != null) {
console.log("match found at: " + match.index+ " length: " +match[0].length);
}
output:
match found at: 0 length: 3
match found at: 22 length: 3
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.