简体   繁体   English

如何替换两个符号之间所有出现的字符串?

[英]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 like This 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. 显然不是我们想要的。

/(<.*?>)/

Just use this. 只是使用这个。 Replace all the occurrences with "" . 将所有出现的内容替换为""

See demo. 参见演示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何替换字符串中第一次出现的模式 - How to replace all BUT the first occurrence of a pattern in string 如何使 lodash _.replace 所有出现在字符串中? - How make lodash _.replace all occurrence in a string? 字符串替换符号? - String replace between symbols? 如何在JavaScript中的字符串的最后两个字符之间替换一个字符? - How do I replace a character between the last occurrence of two characters in a string in JavaScript? 正则表达式:匹配两个符号之间的所有字符串但捕获组 - Regex: Match all string but capture groups between two symbols 如何找出两个字符串之间首次出现的位置的位置? - How to find out the position of the first occurrence of the difference between the two string? String.Replace 仅替换匹配字符串的第一次出现。 如何替换*所有*出现? - String.Replace only replaces first occurrence of matched string. How to replace *all* occurrences? 使用Javascript中的正则表达式替换大文本中两个字符之间的每个出现字符串 - Replace every occurrence string between two characters in a large text using regular expression in Javascript 如何在 javascript 中的两个字符串之间替换字符串? - How to replace string between two strings in javascript? 在 JSON 字符串中,给定键的所有字符串值如何替换特定 substring 的任何出现? - Within a JSON string how does one for all string values of a given key replace any occurrence of a specific substring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM