English 中文(简体)
插图、 extract和总结案文
原标题:Get string, extract id, and wrap text

如果我有以下超文本:

<p> some random text :) video?v=E_LkHrerwe4lnhhnrCbo another video?v=567ghf564_-56e</p>
<p> some2 random2 text2 video?v=rt5y5yvbn5y</p>

Using jQuery how can I get the ID which is 8 characters after video?v= so for the first one the id will be E_LkHrer, then wrap the string with a <span id=" +8charID+ "></span>? Thus, for the HTML above it will return

<p> some random text :) <span id="E_LkHrer">video?v=E_LkHrerwe4lnhhnrCbo</span> another <span id="567ghf56">video?v=567ghf564_-56e</span></p>​
<p> some2 random2 text2 <span id="rt5y5yvb">video?v=rt5y5yvbn5y</span> </p>​

ATM I have this, but I don t know where to go from there and I know the jQuery doesn t have much to do with what I need I was just testing if I could try stripping an id from one video?v=

$("p").each(function() {
  var content = $(this).html();
  var getwatch = content.split( video?v= );
  $(this).after(getwatch[1].substring(0, 8));
});​
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p> some random text :) video?v=E_LkHrerwe4lnhhnrCbo another video?v=567ghf564_-56e</p>
<p> some2 random2 text2 video?v=rt5y5yvbn5y</p>
最佳回答
$( p ).html(function (_, str) {
  return str.replace(/video?v=([^s<]+)/g, function ($0, $1) {
    return [ <span id=" , $1.substr(0, 8),  "> , $0,  </span> ].join(  );
  });
});​

http://jsfiddle.net/HcAyd/1/"rel=“nofollow> http://jsfiddle.net/HcAyd/1/

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签