Javascript Regex, чтобы удалить только тег <html> - PullRequest
0 голосов
/ 06 мая 2020

У меня очень длинная строка, состоящая из нескольких HTML документов, скрепленных вместе вот так:

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
some head info 
</head>
<body>
<div > some content with other HTML tags that I want to preserve </div>
<body>
</html>
<html>
<div> another content with other HTML tags that I want to preserve </div>
</html>
<html xmlns="http://www.w3.org/TR/REC-html40">
<head>
some head info 
</head>
<body>
<div> some other content with other HTML tags that I want to preserve </div>
<body>
</html>

, и я хотел бы превратить их во что-то вроде этого:

<div > some content with other HTML tags that I want to preserve </div>
<div> another content with other HTML tags that I want to preserve </div>
<div> some other content with other HTML tags that I want to preserve </div>

В основном я ищу Regex для удаления только тегов <html> </html> (а не других / внутренних html элементов) из огромной строки html. Обратите внимание, что я должен сохранить содержимое html и просто избавиться от родительских тегов.

Заранее спасибо

(Обратите внимание, что я провел обширный поиск, чтобы убедиться, что это не повторяющийся вопрос)

1 Ответ

0 голосов
/ 06 мая 2020

В качестве важного примечания: { ссылка }

Но если вам нужно, я могу использовать что-то вроде /<\/?html.*?>/g

const html = `<html xmlns:v="urn:schemas-microsoft-com:vml">
<head>head info</head>
<div>other content</div>
</html>`;

console.log(html.replace(/<\/?html.*?>/g, '').trim());

И для настройки регулярного выражения: https://regex101.com/r/EeTv68/1

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...