jquery - find position of tag inside a string javascript -


thanks in advance. stuck problem. have string this

var string = "this <span>i got stuck</span> , <span>clueless</span> can <span>help please</span> "; 

i want know position of each occurrence of span tag in string. in example string have 1 span tag after 3rd word, 1 after 9th word , 1 after 12th word. result array [3, 9, 12]. please me.

edit : also, can able have number of words inside each span tag in addition above array?

as have tagged javascript/jquery, 1 way use .each() , split()

var string = "this <span>i got stuck</span> , <span>clueless</span> can <span>help please</span> ";  //count position var pos = []; string.split(' ').foreach(function(val,i){     if(val.indexof('<span>') > -1){         pos.push(i)     } });  //count words inside each  `<span>` var wordcount = $('<div/>').html(string).find('span').map(function(){     return this.innertext.split(' ').length }) console.log(wordcount) 

demo


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -