答案 0 :(得分:2)
使用正则表达式替换
input.replace(/</g, '<span_').replace(/>/g,'</span>').replace(/_/g,'>');
答案 1 :(得分:1)
尝试这样的事情:
var input = "this is <title> and <heading>";
var output = input.replace(/<([^>]+)>/g, "<span>$1</span>");
console.log(output);
&#13;
也就是说,匹配一个开头<
,后跟一个或多个非>
字符作为子匹配捕获,以便您可以在替换字符串中将其引用为$1
,然后截止>
。
答案 2 :(得分:1)
使用
input.replace(/<[a-zA-Z]*>/g, '<span>$&</span>');
说明:
.replace
将所有代码都包含在&#39;&#39;中。 '$&
已替换为标记。有关详情,请参阅此处:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
示例:
var ta = document.getElementById('ta'), output = document.getElementById('output');
ta.value = 'I have <title> and <head> (this is just example text, you can replace).';
function replaceIt() {
output.innerText = ta.value.replace(/<[a-zA-Z]*>/g, '<span>$&</span>');
// Okay, technically Node#innerText is non-standard, so use an HTML escape function on production sites.
// For demo purposes, I just chose innerText for ease of use
}
&#13;
<textarea id="ta"></textarea>
<button onclick="replaceIt()">Enclose tags in <span> s.</button>
<div>Output:</div>
<div id="output"></div>
&#13;
答案 3 :(得分:1)
另一种选择
import akka.stream.scaladsl.Source
import play.api.libs.streams.Streams
import play.http._
val source = Source.fromPublisher(Streams.enumeratorToPublisher(enumerator))
Ok.sendEntity(HttpEntity.Streamed(source, None, Some("application/zip")))