我需要从字符串中删除span标记。是否可能。?
我的输入是
In open <span class="">court</span> at 11:01 a.m.)
THE <span class="">COURT</span>: Our interpreter.
(NOTE: Spanish interpreter Rosa Lopez-Gaston sworn.)
SPEAKER 2: Judy Faviell for the State.
SPEAKER 1: Carey Bhalla for Mr. Garcia, who is present in custody, Your Honor.
SPEAKER 2: Judge, we do have a change of plea, which I will tender to the <span class="">Court</span>.
THE <span class="">COURT</span>:
输出是
In open court at 11:01 a.m.)
THE COURT: Our interpreter.
(NOTE: Spanish interpreter Rosa Lopez-Gaston sworn.)
SPEAKER 2: Judy Faviell for the State.
SPEAKER 1: Carey Bhalla for Mr. Garcia, who is present in custody, Your Honor.
SPEAKER 2: Judge, we do have a change of plea, which I will tender to the <span class="">Court</span>.
THE COURT:
答案 0 :(得分:21)
对于您编辑过的问题,您可以执行以下操作:
var str = // your string here
str = str.replace(/<\/?span[^>]*>/g,"");
答案 1 :(得分:2)
尝试
var mystring = 'In open <span class="">court</span> at 11:01 a.m.) THE <span class="">COURT</span>: Our interpreter. (NOTE: Spanish interpreter Rosa Lopez-Gaston sworn.) SPEAKER 2: Judy Faviell for the State. SPEAKER 1: Carey Bhalla for Mr. Garcia, who is present in custody, Your Honor. SPEAKER 2: Judge, we do have a change of plea, which I will tender to the <span class="">Court</span>.THE <span class="">COURT</span>:';
var after = $('<div />').html(mystring).find('span').contents().unwrap().end().end().html();
演示:Fiddle
答案 2 :(得分:1)
如果您需要转换的“字符串”已经是HTML元素内容的一部分,那么这是POV(普通老式)JavaScript(即非jQuery)解决方案:
element.outerHTML = element.innerHTML;
例如,如果您想解包/删除文档中的所有跨度,则可以遍历所有相关元素,如下所示:
document.querySelector('span').forEach(spanElmt => {
spanElmt.outerHTML = spanElmt.innerHTML;
};
下面的示例演示了它是如何工作的,表明指定的元素已删除/展开,而嵌套在其中的任何元素均保持不变。
const btn = document.querySelector('button');
const redSpan = document.querySelector('.red' );
const par = document.querySelector('p' );
const div = document.querySelector('div' );
const unwrapElmt = elmt => {
elmt.outerHTML = elmt.innerHTML; // *** the key line
};
const showParagraphHTML = () => {
div.textContent = 'HTML for the above line: ' + par.innerHTML;
};
showParagraphHTML();
const unwrapRedSpan = () => {
unwrapElmt(redSpan);
showParagraphHTML();
};
btn.addEventListener('click', unwrapRedSpan);
.red {
color: red;
}
.italic {
font-style: italic;
}
<button>Click here to unwrap/remove red span</button>
<p>Here is <span class="red">some <span class="italic">very</span> red</span> text.</p>
<p><div></div>
此解决方案要求要修改的文本/字符串已经是HTML文档或元素的一部分。因此,它不适用于以下独立字符串:
const myStr = 'some <span><i>very</i> interesting</span> thing';
myStr.outerHTML = myStr.innerHTML; // won't work
最终,这就是原始问题在问什么。但是,可以将上面的字符串作为临时的中间孤立HTML容器元素的一部分,之后此解决方案将变得可用:
const myStr = 'some <span><i>very</i> interesting</span> thing';
const container = document.createElement('div'); // a temporary orphan div
container.innerHTML = myStr;
const spanToUnwrap = container.querySelector('span');
spanToUnwrap.outerHTML = spanToUnwrap.innerHTML;
const myModifiedStr = container.innerHTML;
例如,如果您只想从单个字符串中删除一个或几个简单标签(如问题中所建议的),这似乎就显得有些过头了。在这种情况下,基于正则表达式的替换可能就足够了,例如another answer中提出的替换。但是,对于任何更复杂的情况(例如,只想删除特定类的跨度等),将临时的孤立HTML元素用作中间元素可能会很有用。
请注意,本节描述的策略最终是a different answer所展示的如何使用jQuery的方法。但是,如果您的初始字符串已经是HTML元素的一部分,并且您还不需要jQuery,那么当您实际上只需要进行outerHTML
/ innerHTML
交换时,使用jQuery可能就显得过头了。>
答案 3 :(得分:0)
这应该这样做
return $(string).text()
如果你不使用jQuery,你可以这样:
var e = document.createElement('div');
e.innerHTML = string;
return e.textContent ? e.textContent : e.innerText;
您也可以尝试使用DOMParser,但可能会因您正在尝试的事情而过度杀伤