javascript - Removing everything between two strings regex - Stack Overflow

I have the following:<span class="hello" id="something somemore"> blah blah b

I have the following:

<span class="hello" id="something somemore"> blah blah blah </span>

My regex is not my strongest ability, would appreciate is someone could help me in javascript regex.

I would like to remove everything/anything and including from <span to > and then finally just remove the </span>.

I am using .replace(myregexhere, ""); to replace other items but I am struggling to do the above.

Also if there are easier or better alternatives let me know.

I have the following:

<span class="hello" id="something somemore"> blah blah blah </span>

My regex is not my strongest ability, would appreciate is someone could help me in javascript regex.

I would like to remove everything/anything and including from <span to > and then finally just remove the </span>.

I am using .replace(myregexhere, ""); to replace other items but I am struggling to do the above.

Also if there are easier or better alternatives let me know.

Share Improve this question asked Aug 7, 2012 at 16:28 AnichoAnicho 2,66711 gold badges48 silver badges76 bronze badges 1
  • why not just remove the element itself? – Phillip Schmidt Commented Aug 7, 2012 at 16:30
Add a ment  | 

4 Answers 4

Reset to default 6

If you really want to use regex replace, try this:

var newstring = oldstring.replace(/<span[^>]*>([^<]*)<\/span>/, "$1");

Note that this doesn't work if any html tags are in between <span> and </span>. If you have html tags there then please use something like this instead.

You want something like:

~<span.+?>~

and

~</span>~

Why not use replaceWith() instead :

var something = $('#something').text();
$('#something').replaceWith(something);

use jQuery:

$('span').remove();

or for each span with class "hello":

$('span .hello').remove();

or for each element with id "something somemore",

$("#something somemore").remove();

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信