简体   繁体   English

替换非 html 并用正确的语法替换它

[英]Replace non-html and replace it with correct syntax

I have a source program that delivers text with non html tags and incorrect syntax.我有一个源程序,它提供带有非 html 标记和不正确语法的文本。 for example:例如:

the <H>quick</> brown fox.
the <U>quick</> brown fox.
<H><U>The</> quick brown fox.
<H><U>The</> quick </> brown fox.

The out come should be someting like:结果应该是这样的:

the quick brown fox.敏捷的棕色狐狸。

the quick brown fox.敏捷的棕色狐狸。

The quick brown fox.敏捷棕色狐狸。

The quick brown fox.敏捷棕色狐狸。

So the tags used are not html-valid, but also not closed as they should.所以使用的标签不是 html-valid,也不是应该关闭的。 I'm struggling to get this working in javascript.我正在努力让它在 javascript 中工作。

started with something like:从以下内容开始:

var s = document.getElementById('root').innerHTML;
s = s.replace("&lt;H&gt;", "<b>");  
s = s.replace("&lt;h&gt;", "<b>");    
s = s.replace("&lt;/&gt;","</b>");   
document.getElementById('root').innerHTML = s;

root is the all containing div. root 是所有包含的 div。 The tags will appier in a div with class "label components", there will be multiple divs with class "label components" (and thus multiple times the incorrect tags on a page).标签将出现在带有 class “标签组件”的 div 中,将有多个带有 class “标签组件”的 div(因此页面上的错误标签多次出现)。

how can I best tackle this?我怎样才能最好地解决这个问题?

Probably easiest to write a small parser/processor that uses a stack to keep track of the tags that still need to be closed:可能最容易编写一个使用堆栈来跟踪仍需要关闭的标签的小型解析器/处理器:

 const s1 = 'the <H>quick</> brown fox.'; const s2 = 'the <U>quick</> brown fox.'; const s3 = '<H><U>The</> quick brown fox.'; const s4 = '<H><U>The</> quick </> brown fox.'; const process = (s) => { const map = {'H': 'b', 'U': 'i'}; const stack = []; return s.replace(/<([AZ/])>/g, (_, t) => { if (map[t]) { stack.push(map[t]); return `<${map[t]}>`; } else { return `</${stack.pop()}>`; } }); }; console.log(process(s1)); console.log(process(s2)); console.log(process(s3)); console.log(process(s4));

Your third example still comes out to be invalid HTML, because of the fact that the number of opening and closing tags doesn't match.您的第三个示例仍然无效 HTML,因为打开和关闭标签的数量不匹配。 If that's more than just a mistake in your example, you'll be looking at a more complex solution, and would need to specify what the desired behavior is supposed to be.如果这不仅仅是您的示例中的错误,您将看到一个更复杂的解决方案,并且需要指定期望的行为应该是什么。

In my opinion the best route in this case would be to open html document in editor like VS Code and use find and replace tool .在我看来,在这种情况下,最好的方法是在 VS Code 等编辑器中打开 html 文档并使用查找和替换工具 You should also use HTMLHint exten sion for VS Code to highlight all the problems in html document.您还应该使用 VS Code 的HTMLHint 扩展来突出显示 html 文档中的所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM