英文:
Regex replace html and add spacing on div
问题
I understand your request. Here's the translated content:
你好,我想要创建一个正则表达式,用于替换所有的 HTML 标签,但当相邻的标签是结束的 <div>
和开始的 <div>
时,它会添加一个空格。例如:
This <b>is</b> <div>a</div><div>test</div>
应该变成:
This is a test
我目前的正则表达式是 /(<([^>]+)>)/ig
,它可以替换所有的 HTML 标签,但我想知道如何在相邻的 <div>
标签之间添加空格。
我尝试使用 /(<([^>]+)>)/ig
来替换 HTML,它可以工作,但我需要帮助在相邻的 <div>
标签之间添加空格。
英文:
Hello I would like help creating a regex that replaces all html tags but when there is an end div and start div next to each other it adds a space, so for example
This <b>is</b> <div>a</div><div>test</div>
This is a test
What I currently have for regex is /(<([^>]+)>)/ig which will replace all html tags but Im wondering how do I also add a space whenever there is a closing div and starting div next to each other.
I tried using /(<([^>]+)>)/ig to replace the html which works but I need help with the spacing on divs when they are next to each other
答案1
得分: 1
JS具有内置的HTML解析支持。请使用它:
function getSpaceSeparatedText(html) {
// 创建一个元素并将其用作解析器
let parser = document.createElement('div');
parser.innerHTML = html;
const result = [];
for (const node of parser.childNodes) {
// 获取修剪后的文本
const text = node.textContent.trim();
// 如果文本不为空,则添加到结果中
if (text) {
result.push(text);
}
}
return result.join(' ');
}
尝试一下:
function getSpaceSeparatedText(html) {
let parser = document.createElement('div');
parser.innerHTML = html;
const result = [];
for (const node of parser.childNodes) {
const text = node.textContent.trim();
if (text) {
result.push(text);
}
}
return result.join(' ');
}
const html = `
This <b>is</b>
<div>a</div><div>test</div>
`;
console.log(getSpaceSeparatedText(html));
英文:
JS has built-in support for HTML parsing. Use that instead:
function getSpaceSeparatedText(html) {
// Create an element and use it as a parser
let parser = document.createElement('div');
parser.innerHTML = html;
const result = [];
for (const node of parser.childNodes) {
// Get the trimmed text
const text = node.textContent.trim();
// If text is not empty, add it to result
if (text) {
result.push(text);
}
}
return result.join(' ');
}
Try it:
<!-- begin snippet: js hide: true -->
<!-- language: lang-js -->
console.config({ maximize: true });
function getSpaceSeparatedText(html) {
let parser = document.createElement('div');
parser.innerHTML = html;
const result = [];
for (const node of parser.childNodes) {
const text = node.textContent.trim();
if (text) {
result.push(text);
}
}
return result.join(' ');
}
const html = `
This <b>is</b>
<div>a</div><div>test</div>
`;
console.log(getSpaceSeparatedText(html));
<!-- language: lang-html -->
<script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<!-- end snippet -->
答案2
得分: 0
更新:将新组添加到顶部会导致后续反向引用偏移一个。
问题已修复。
这会移除所有HTML标签和不可见内容(https://regex101.com/r/2ACiDg/1),但您需要一个回调在关闭div和打开div之间插入一个空格。
英文:
Update: Adding a new group to the top caused an offset by one to the subsequent backreferences.
Was fixed.
This removes all HTML tags and invisible content (https://regex101.com/r/2ACiDg/1),
but you need a callback to insert a space between a closing and open div.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var text = "This <b>is</b> <div>a</div><div>test</div>"
text = text.replace(/(<\/div\s*><div\s*>)|<(?:(?:(?:(script|style|object|embed|applet|noframes|noscript|noembed)(?:\s+(?=((?:"[\S\s]*?"|'[\S\s]*?'|(?:(?!\/>)[^>])?)+)))?\s*>)[\S\s]*?<\/\s*(?=>))|(?:\/?[\w:]+\s*\/?)|(?:[\w:]+\s+(?:"[\S\s]*?"|'[\S\s]*?'|[^>]?)+\s*\/?)|\?[\S\s]*?\?|(?:!(?:(?:DOCTYPE[\S\s]*?)|(?:\[CDATA\[[\S\s]*?\]\])|(?:--[\S\s]*?--)|(?:ATTLIST[\S\s]*?)|(?:ENTITY[\S\s]*?)|(?:ELEMENT[\S\s]*?))))>/g, function(match, grp1)
{
if ( grp1 > "" )
return " ";
else
return ""
}
);
console.log( text );
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论