[英]how to replace all occurrence of string between two symbols?
I'm working with RegEx on Javascript and here is where I stuck. 我正在使用Java的RegEx,这就是我要坚持的地方。
I have a simple string like 我有一个简单的字符串
<html><body><span style=3D"font-family:Verdana; color:#000; font-size:10pt;=
"><div><font face=3D"verdana, geneva" size=3D"2">http://72.55.146.142:8880/=
order003.png.zip,120</body></html>
all i need to do is write javascript which can replace all strings in with "<" and ">" symbol. 我需要做的就是编写javascript,它可以将所有字符串替换为“ <”和“>”符号。
I wrote something like this - 我写了这样的东西-
var strReplaceAll = Body;
var intIndexOfMatch = strReplaceAll.indexOf( "<" );
while (intIndexOfMatch != -1){
strReplaceAll = strReplaceAll.replace(/<.*>/,'')
intIndexOfMatch = strReplaceAll.indexOf( "<" );
}
but the problem is if body contains - 但是问题是身体是否包含-
test<abc>test2<adg>
it will give me - 它会给我-
test
only or if body contains like - 仅或如果主体包含-
<html>test<abc>test2<adg>
it will give me nothing please let me know how i can get- 它不会给我任何东西,请让我知道我如何能-
testtest2
as a final output. 作为最终输出。
Try this regex instead: 尝试使用此正则表达式:
<[^>]+>
DEMO: 演示:
http://regex101.com/r/kI5cJ7/2 http://regex101.com/r/kI5cJ7/2
DISCUSSION 讨论
Put the html code in a string and apply to this string the regex. 将html代码放在字符串中,然后将正则表达式应用于此字符串。
var htmlCode = ...;
htmlCode = htmlCode.replace(/<[^>]+>/g, '');
The original regex take too much characters ( *
is a greedy operator). 原始正则表达式使用太多字符( *
是贪婪的运算符)。
Check this page about Repetition with Star and Plus , especially the part on "Watch Out for The Greediness!" 检查有关Star和Plus重复的页面,尤其是“当心贪婪!”部分。 . 。
Most people new to regular expressions will attempt to use
<.+>
. 正则表达式的大多数新手都会尝试使用<.+>
。 They will be surprised when they test it on a string likeThis is a <EM>first</EM> test
. 当他们在像This is a <EM>first</EM> test
的字符串上测试它时会感到惊讶,This is a <EM>first</EM> test
。 You might expect the regex to match<EM>
and when continuing after that match,</EM>
. 您可能希望正则表达式匹配<EM>
并且在匹配之后继续</EM>
。But it does not. 但事实并非如此。 The regex will match
<EM>first</EM>
. 正则表达式将匹配<EM>first</EM>
。 Obviously not what we wanted. 显然不是我们想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.