Javascript使用&lt; span&gt;标题&lt; / span&gt;替换<title>中的文本

时间:2016-12-12 06:01:17

标签: javascript html replace

&lt; p&gt;您好我想替换我的javascript字符串解决方案。&lt; / p&gt; &lt; pre&gt;&lt; code&gt;输入: - 这是&lt; title&gt;和&lt; heading&gt; 输出: - 这是&lt; span&gt;标题&lt; / span&gt;和&lt; span&gt;标题&lt; / span&gt; &LT; /代码&GT;&LT; /预&GT; &lt; p&gt;任何人都可以帮助我。 在此先感谢。&lt; / p&gt;

4 个答案:

答案 0 :(得分:2)

使用正则表达式替换

input.replace(/</g, '<span_').replace(/>/g,'</span>').replace(/_/g,'>');

答案 1 :(得分:1)

尝试这样的事情:

&#13;
&#13;
var input = "this is <title> and <heading>";
var output = input.replace(/<([^>]+)>/g, "<span>$1</span>");
console.log(output);
&#13;
&#13;
&#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

示例:

&#13;
&#13;
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 &ltspan&gt s.</button>
<div>Output:</div>
<div id="output"></div>
&#13;
&#13;
&#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")))
相关问题