jquery - find position of tag inside a string javascript - Stack Overflow

Thanks in advance. I am stuck with a problem. I have a string like thisvar string = "This is where

Thanks in advance. I am stuck with a problem. I have a string like this

var string = "This is where <span>I got stuck</span> and I am <span>clueless</span> can anyone <span>help please</span> ";

I want to know the position of each occurrence of the span tag in the string. In the example string i have one span tag after 3rd word, one after 9th word , one after the 12th word. so the result array will be [3, 9, 12]. Please help me.

Edit : Also, can i be able to have the number of words inside each span tag in addition to the above array?

Thanks in advance. I am stuck with a problem. I have a string like this

var string = "This is where <span>I got stuck</span> and I am <span>clueless</span> can anyone <span>help please</span> ";

I want to know the position of each occurrence of the span tag in the string. In the example string i have one span tag after 3rd word, one after 9th word , one after the 12th word. so the result array will be [3, 9, 12]. Please help me.

Edit : Also, can i be able to have the number of words inside each span tag in addition to the above array?

Share Improve this question asked Jun 20, 2015 at 7:02 ArumaiArumai 754 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

As you have tagged javascript/jQuery, one way would be to use .each() and split()

var string = "This is where <span>I got stuck</span> and I am <span>clueless</span> can anyone <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

You can check the string using a regex like this:

var string = "This is where <span>I got stuck</span> and I am <span>clueless</span> can anyone <span>help please</span> ";
var regex = /<span>/gi, result, indices = [];
while ((result = regex.exec(string))) {
     console.log(result.index);
}

This will print you every position of the tag <span>. You can adapt the While code and it should do the rest. :-)

You can use:

var string = "This is where <span>I got stuck</span> and I am <span>clueless</span> can anyone <span>help please</span> ";

var arr = string.split(/[\s>]+/);

var posn = [];
for(var p=-1; (p=arr.indexOf('<span', p+1)) >= 0; )
   posn.push(p)

//=> [3, 10, 14]

I think you are looking for this

var string = "This is where <span>I got stuck</span> and I am <span>clueless</span> can anyone <span>help please</span> ";

var prevPos = 0;

var pos = [];
var words = [];
string.split(' ').forEach(function(val,i){
    if(val.indexOf('<span>') > -1){
        pos.push(i);
        var wordsCount = i - prevPos;
        prevPos = i; 
        words.push(wordsCount);

    }
});

console.log(pos);
console.log(words);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745606869a4635720.html

相关推荐

  • jquery - find position of tag inside a string javascript - Stack Overflow

    Thanks in advance. I am stuck with a problem. I have a string like thisvar string = "This is where

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信