如何替换 html 标记属性字符串中的多个“<”和“>”
Howto replace multiple '<' and '>' inside html-tag attribute string?
我从 rte 读取内容,在将它们提交到服务器之前,我需要将<
和>
替换为某些标题属性中的 html 实体。我不想在这里使用 Dom-Operations,因为文本表示就是我在这里得到的全部。我正在寻找的是一个改变这个的正则表达式
<div>ABCD<img style="max-height: 25px; max-width: 25px;" class="inlinetag"
src="http://my_images/icon.gif"
title="<ir_inline itemname=bild_1 type=0><cbd>"> EFG</div>
进入这个
<div>ABCD<img style="max-height: 25px; max-width: 25px;" class="inlinetag"
src="http://my_images/icon.gif"
title="<inline itemname=bild_1 type=0><cbd>"> EFG</div>
如何做到这一点?
纯正则表达式解决方案:
var input = "title='"<ir_inline itemname=bild_1 type=0><cbd>'""; //use the entire input
var myRegexp = /title='"(.*?)'"/g; // get all title attributes
var output = input.replace(myRegexp,function(a){return a.replace(/</g,"<").replace(/>/g,">");});
我已经使用您的示例输入和输出对其进行了测试。它应该有效。
所以让我假设几件事,你有一个带有html标签和属性的纯文本,你只想把它当作纯文本,可能是因为你在服务器端得到这个文本。
除了正则表达式之外,如果您更喜欢通过循环进行字符串操作,那么下面是简单的循环(逻辑),通过它您可以实现您想要的。
我假设你需要在服务器端做这件事,所以我为此目的使用了 C#,你可以使用任何语言,甚至是 javascript 来执行这个循环。
string sourceText = "<div id='"target'" ><div>ABCD<img style='"max-height: 25px; max-width: 25px;'" class='"inlinetag'" " +
"src='"http://my_images/icon.gif''" +
"title='"<ir_inline itemname=bild_1 type=0><cbd>'"> EFG</div>" +
"</div>";
string targetText = sourceText;
bool traceOn = false;
for (int i = 0; i < targetText.Length; i++)
{
if (traceOn)
if (targetText[i] == '"')
traceOn = false;
if (traceOn)
{
if (targetText[i] == '<')
{
targetText = targetText.Remove(i, 1).Insert(i, "<");
}
if (targetText[i] == '>')
{
targetText = targetText.Remove(i, 1).Insert(i, ">");
}
}
if (targetText[i] == '"')
{
if (targetText[i - 1] == '=')
traceOn = true;
}
}
}
所以基本上我正在做的是操纵您的替代品的模式,即您只需要替换那些出现在双引号内且前面带有"="的<和>。它工作得很好。和>
这不是一个完美的解决方案,但它应该给你和想法,你如何处理你的字符串。 这里有人可以写出更强大、更灵活的逻辑。 尝试/改进它。
其他解决方案可以是,将你的整个字符串视为xml。几乎所有服务器端语言都提供将字符串作为 XML 处理的工具。找到适合您需求的产品,即
我本可以做这样的事情
XmlDocument doc = new Xmldocument();
doc.LoadXml(targetString);
and then I could easily retrieve any tag and its attribute.
至于正则表达式,我非常害怕它们。它应该给你一个想法。
试试这个JS函数:
function title_replace() {
var str = '<div>ABCD<img style="max-height: 25px; max-width: 25px;" class="inlinetag" '
src="http://my_images/icon.gif" '
title="<ir_inline itemname=bild_1 type=0><cbd>"> EFG</div>';
var re = new RegExp(/title="(.|['s'S])+?"/gm);
var title = "";
while (title_matches = re.exec(str)) {
title = title_matches[0];
var new_title = title.replace(/</gm,"<").replace(/>/gm,">");
str = str.replace(title, new_title);
}
}
编辑:
我已经删除了 dom 上的所有工作,现在都是 JS。看看这是否适合您。
在服务器端尝试反萨米。它功能强大且安全。
相关文章:
- 字符串替换-不支持格式字符'}'
- Unicode字符串替换不适用于完整的变音符号阿拉伯文本
- Javascript 将字符串替换为
返回为空 - 字符串替换只替换第一个匹配项,可以'不要让它全球化
- javascript字符串替换$1(如果它是某个值)
- 字符串替换正则表达式,开头为空格
- 将 url 中的一个字符串替换为另一个字符串
- 在HTML文本框中查找某些子字符串,然后用其他字符串替换它们
- 我想使用replace函数扫描json对象,然后用字符串替换匹配的单词
- 字符串替换为从右向左的语言(阿拉伯语)
- 将字符串替换为用户键入的文本
- 字符串替换javascript
- JavaScript 将多行字符串替换为多行字符串
- Javascript字符串替换不起作用
- 字符串替换不'当通过android webView JavascriptInterface传递字符串时无法工作
- 我正在尝试用另一个字符串替换一个字符串
- 字符串替换最后一个字符出现的斜杠
- 将具有的字符串替换为$
- 基于查询字符串替换HTML
- Javascript字符串替换在满足长度而不是instantanoeus之前不会生效