截断 <style>从字符串标记数据 [英] Truncate <style> tag data from a string
本文介绍了截断 <style>从字符串标记数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,比如说 headData,它是 和
标签的组合.对于 Ex(with Dummy data) -
I have a string let's say headData which is combination of <script>
and <style>
tags. For Ex(with Dummy data) -
let headData = '<style>
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 300;
src: local('Roboto Light'), local('Roboto-Light'), url(path-to.woff) format('woff');
}</style>
<script>var isPresent = false;</script>
<script>var isContent = true;</script>
<style>@font-face {
font-family: 'Courgette';
font-style: normal;
font-weight: 400;
src: local('Courgette Regular'), local('Courgette-Regular'), url(path-to.woff2) format('woff2');}</style>'
我在标签中注入了整个 headData,如下所示.
I inject whole of headData in tag like below.
<script dangerouslySetInnerHTML={{__html: headData}} />
我不想注入 标签相关数据,只想注入所有
标签相关数据.
I don't want to inject <style>
tag related data and only want all the <script>
tag related data to be injected.
所以我最终想要注入的是类似于 -
So what I finally want to inject is similar to -
let headData = '<script>var isPresent = false;</script>
<script>var isContent = true;</script>'
在 Javascript 中实现这一目标的正确方法是什么?
What is the right way to achieve this in Javascript?
推荐答案
您可以使用带有 Capturing Groups 的 RegEx 来替换 style 标签及其所有出现的内容:
You can use RegEx with Capturing Groups to replace the style tag and it's content for all occurrences:
/(<style>)[^<>]*(<\/style>)/g
地点:
(