使用java脚本删除指定标记之间的内容 [英] removing content between specified tokens using java script

查看:78
本文介绍了使用java脚本删除指定标记之间的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi

我想删除< script>中嵌入的内容和< / script>标签

通过文本框提交。

我的java脚本应该删除嵌入在< script>之间的内容。和

< / script>标签。

我目前的代码是


函数RemoveHTMLScript(strText)

{

var regEx = /< script \w *< \ / script> / g

返回strText.replace(regEx,"");

}

让我们说,

strText =" Hi< script> .... .... .....< / script>你怎么样?$

预期的输出是你好,你好吗


正常表达式解决方案是首选

谢谢和问候

Raja rao

解决方案

" rajarao" < RA ****** @ yahoo.com>写道:

我想删除< script>中嵌入的内容和< / script>标签
通过文本框提交。
我的java脚本应该删除嵌入在< script>之间的内容。和
< / script>我的当前代码是

函数RemoveHTMLScript(strText)
{var /> var regEx = /< script\w *< \ / script> / g


这匹配"< script"然后是零或多个word

字符。单词字符不包含>,所以这不太可能是
工作。

返回strText.replace(regEx,"");
}
让我们说,
strText =" Hi< script> .... .... .....< / script>你怎么样?;
预期的输出是你好,你好吗?


更有可能你好,你好吗,如果一个人需要迂腐,显然

我这样做:)

正则表达式解决方案是首选




首先要考虑的是如果文本是:


" abc< ; script> ...< / script> def< script> ...< / script> ghi"


您可能希望将其简化为abcdefghi。 。但是,如果您使用与< script>匹配的简单regualar表达式,则



< / script>,它将匹配第一个< script>到最后一个< / script>,

只返回abcghi。


为了避免这种情况,你需要通过普通的非贪婪匹配

表达式,仅适用于最近的浏览器。你不是说

这个代码是应该在网页上还是在服务器上执行,

但如果是在服务器上,你可以控制版本Javascript和

可以依赖非贪婪匹配(如果有的话)。


然后尝试这个RegExp:

/< \\ \\ s * script。+?< \ / \\\ * script \s *> / ig


如果没有非贪婪的正则表达式,你可以找到

实例使用indexOf手动。但是,它不是很有效,因为它不会忽略大小写和空格。它可以工作,

但它不是那么有趣:)

/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL: http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''


< blockquote> Lasse Reichstein Nielsen写道:

" rajarao" < RA ****** @ yahoo.com>写道:

首选正则表达式解决方案首先要考虑的是如果文本是:

< abc< script> ...< ; / script> def< script> ...< / script> ghi"

您可能希望将其简化为abcdefghi。但是,如果您使用来自< script>的简单的regualar表达式匹配,那么

< / script>,它将匹配第一个< script>到最后< / script>,
只返回abcghi。

为了避免这种情况,你需要通过常规的
表达式进行非贪婪的匹配,在最近的浏览器中可用你不是说这个代码是应该在网页上还是在服务器上执行,
如果是在服务器上,你可以控制Javascript的版本,
可以如果可用的话,依靠非贪婪的匹配。

然后尝试这个RegExp:
/<\s*script.+?<\/\s*script\ *> / ig




那里真的有一个UA是如此b5rken来解析"<脚本>" as

"< script>"和< / script>作为< / script>? HTML的SGML声明

明确禁止所有元素。 "<"是STAGO(开始标记打开)和

"< /"是ETAGO(结束标记打开),其中两个都不能跟着白色

空格。

如果没有非贪婪的正则表达式,你可以找到
使用indexOf手动实例。但是,它不是很有效,因为它不会忽略大小写和空格。它可以工作,
但它并没有那么有趣:)




这就是为什么要使用


/< script [^>] *> [^<>] *< \ / script> / ig


then 。由于这不是我第一次遇到问题,

我将扩展我的stripTags()方法[1],这样你就可以只删除

只有特定的标签和如果你愿意的话也可以提供他们的内容。

PointedEars

___________

[1]< http://pointedears.de.vu/scripts/ string.js>


Thomas''PointedEars''Lahn< Po ********* @ web.de>写道:

那里真的有一个UA是如此的解析<<脚本>" as
"< script>"和< / script>作为< / script>?


可能:)但我不知道。


这就是为什么人们想要使用

/< script [^>] *> [^<>] *< \ / script> / ig




排除:

---

< script type =" text / javascript">

if(screen.innerWidth< 1000) {alert(" your resolution sucks);}

< / script>

---

因为它包含 <"在剧本里面。

你应该匹配< /为了正确,或者最多为< / script

以符合浏览器的要求。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< ;网址:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''


hi
I want to remove the content embedded in <script> and </script> tags
submitted via text box.
My java script should remove the content embedded between <script> and
</script> tag.
my current code is

function RemoveHTMLScript(strText)
{
var regEx = /<script\w*<\/script>/g
return strText.replace(regEx, "");
}
let us say,
strText = "Hi <script> .... .... ..... </script> How are u";
the expected out put is "Hi How are u"

Regular expression solution is preferred
thanks and regards
Raja rao

解决方案

"rajarao" <ra******@yahoo.com> writes:

I want to remove the content embedded in <script> and </script> tags
submitted via text box.
My java script should remove the content embedded between <script> and
</script> tag.
my current code is

function RemoveHTMLScript(strText)
{
var regEx = /<script\w*<\/script>/g
This matches "<script" followed by zero or more "word
characters". Word characters doesn''t include ">", so this is unlikely
to work.
return strText.replace(regEx, "");
}
let us say,
strText = "Hi <script> .... .... ..... </script> How are u";
the expected out put is "Hi How are u"
More likely "Hi How are u", if one needs to be pedantic, as evidently
I do :)
Regular expression solution is preferred



First thing to consider is what to do if the text is:

"abc<script>...</script>def<script>...</script>ghi"

You would probably want this to be simplified to "abcdefghi". However,
if you use a simple regualar expression matching from <script> to
</script>, it will match from the first <script> to the last </script>,
returning only "abcghi".

To avoid this, you need a non-greedy matching by the regular
expression, something only available in recent browsers. You don''t say
whether this code should be executed on a web page or on a server,
but if it is on a server, you control the version of Javascript, and
can rely on non-greedy matching if available.

Try this RegExp then:
/<\s*script.+?<\/\s*script\s*>/ig

If non-greedy regular expressions are not available, you can find the
instances manually using indexOf. It''s not very effective, though,
since it doesn''t ignore case and whitespace. It can be made to work,
but it''s not nearly as much fun :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


Lasse Reichstein Nielsen wrote:

"rajarao" <ra******@yahoo.com> writes:

Regular expression solution is preferred
First thing to consider is what to do if the text is:

"abc<script>...</script>def<script>...</script>ghi"

You would probably want this to be simplified to "abcdefghi". However,
if you use a simple regualar expression matching from <script> to
</script>, it will match from the first <script> to the last </script>,
returning only "abcghi".

To avoid this, you need a non-greedy matching by the regular
expression, something only available in recent browsers. You don''t say
whether this code should be executed on a web page or on a server,
but if it is on a server, you control the version of Javascript, and
can rely on non-greedy matching if available.

Try this RegExp then:
/<\s*script.+?<\/\s*script\s*>/ig



Is there really a UA out there that is so b0rken to parse "< script>" as
"<script>" and "</ script>" as "</script>"? The SGML declaration of HTML
clearly forbids that for all elements. "<" is STAGO (Start Tag Open) and
"</" is ETAGO (End Tag Open) where both must not be followed by white
space.
If non-greedy regular expressions are not available, you can find the
instances manually using indexOf. It''s not very effective, though,
since it doesn''t ignore case and whitespace. It can be made to work,
but it''s not nearly as much fun :)



That is why one wants to use

/<script[^>]*>[^<>]*<\/script>/ig

then. Since this is not the first time I encountered the problem,
I am going to extend my stripTags() method[1] so that you can strip
only specific tags and also their content if you want.
PointedEars
___________
[1] <http://pointedears.de.vu/scripts/string.js>


Thomas ''PointedEars'' Lahn <Po*********@web.de> writes:

Is there really a UA out there that is so b0rken to parse "< script>" as
"<script>" and "</ script>" as "</script>"?
Probably :) But I don''t know of any.

That is why one wants to use

/<script[^>]*>[^<>]*<\/script>/ig



That rules out:
---
<script type="text/javascript">
if (screen.innerWidth < 1000) { alert("your resolution sucks");}
</script>
---
since it contains a "<" inside the script.
You should match up to "</" for correctness, or up to "</script"
for compliance with browsers.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


这篇关于使用java脚本删除指定标记之间的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆