在Javascript或jQuery中,如何仅删除第一个和最后一个标签? [英] In Javascript or jQuery, how do I remove only the first and last tag?

查看:46
本文介绍了在Javascript或jQuery中,如何仅删除第一个和最后一个标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下字符串:

var htmlStr = '<p class="red_349dsa01">This is</p><p class="blue_saf9vsaz">a test</p>';

如何删除该字符串的第一个和最后一个标签?结果将是:

how can I remove the very first and last tag off this string? This would be the result:

var htmlStr = 'This is</p><p class="blue_saf9vsaz">a test';

我知道这会创建无效的HTML,但是我只想知道这是否可以完成.

I know this will create invalid HTML, but I just want to know if this can be done at all.

推荐答案

您可以尝试类似

var htmlStr = '<p class="red_349dsa01">This is</p><p class="blue_saf9vsaz">a test</p>';
alert(htmlStr.substring(htmlStr.indexOf('>') + 1, htmlStr.lastIndexOf('<')));

这篇关于在Javascript或jQuery中,如何仅删除第一个和最后一个标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆