Javascript 正则表达式替换 HTML 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12090672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Regex Replace HTML Tags
提问by LmC
Having a lot of difficulties using regex.
使用正则表达式有很多困难。
Heres what i am trying to do...
这就是我想要做的......
text<div> text </div><div> text </div><div> text </div>
to turn it in to
把它变成
text<br> text<br>text<br>text
I've tryed doing...
我试过做...
newhtml = newhtml.replace(/\<div>/g,'<br>');
newhtml = newhtml.replace(/\</div>/g,' ');
but this gives the wrong output. Does jquery provide a better way of doing this?
但这给出了错误的输出。jquery 是否提供了更好的方法来做到这一点?
回答by h2ooooooo
That's because you're escaping the wrong thing, as only the backslash needs to be escaped.
那是因为你在逃避错误的事情,因为只有反斜杠需要被转义。
newhtml = newhtml.replace(/<div>/g,'<br>');
newhtml = newhtml.replace(/<\/div>/g,' ');
回答by Krule
Yes you are correct, jQuery does provide a better way of doing this.
是的,你是对的,jQuery 确实提供了一种更好的方法来做到这一点。
An interesting readfirst.
Easy, elegant, solution to your specific problem.
简单,优雅,解决您的特定问题。
$('div').replaceWith(function(){
return "<br>"+$(this).html();
});?
回答by user907860
This must do the job:
这必须完成这项工作:
text.replace(/(<\/?\w+?>)\s*?(<\/?\w+?>)|(<\/?\w+?>)/g,'<br>')
Though this will only work if there were no tags with some attributes like <div id="foo1">
You do not need to escape <
as you did in your example, but instead you do need to escape /
尽管这仅在没有具有某些属性的标签时才有效,例如<div id="foo1">
您不需要像<
在示例中那样进行转义,但是您确实需要转义/
回答by WhyNotHugo
Don't use regexes if you don't need them; just replace string literals.
如果不需要,请不要使用正则表达式;只需替换字符串文字。
text.replace("<div>","<br>").replace("</div>","");
Note: This solution applies exactly to thisscenario, I don't normally have anything against using regular expresions.
注意:此解决方案完全适用于这种情况,我通常不反对使用正则表达式。
回答by Ricardo Alvaro Lohmann
A simple way to do this is the following:
执行此操作的简单方法如下:
$('.container').html(function(i, html) {
return html.replace(/<(|\/)div>/g, function(match) {
return match == '<div>' ? '<br>' : '';
});
});
/<(|\/)div>/
: Matches <div>
or </div>
.
/<(|\/)div>/
: 匹配<div>
或</div>
。
Note: .container
is where your html is placed.
注意:.container
是放置 html 的位置。
回答by Ashirvad
One Liner using JQuery
使用 JQuery 的一个班轮
newhtml = $(newhtml ).text().split(' ').join('<br/>');
回答by Hasmukh
You can achieve this using a simple RegExp
您可以使用简单的 RegExp 来实现这一点
output = inputText.replace(/<\w{0,}\W{0,}>|<\W{0,}\w{1,}>/ig, "With whatever you want it to be replaced with")
output = inputText.replace(/<\w{0,}\W{0,}>|<\W{0,}\w{1,}>/ig, "With whatever you want it to be replaced with")
Or you can do this
或者你可以这样做
String.prototype.replaceTags = function( replacementText )
{
var x = new RegExp( "(" + replacementText + ")+" , "ig");
return this
.replace( /<\w{0,}\W{0,}>|<\W{0,}\w{1,}>/ig, replacementText )
.replace( x, replacementText )
}
And then call it directly on the String as follows
然后直接在String上调用如下
"text<div> text </div><div> text </div><div> text </div>".replaceTags( "<br>" )
"text<div> text </div><div> text </div><div> text </div>".replaceTags( "<br>" )
You'll get this -- "text<br> text <br> text <br> text <br>"
你会得到这个—— "text<br> text <br> text <br> text <br>"
This will search for portions in the string which begin with the "<" contains some text in between "div/p/br" additionally if the tag is being ended by "/" and finally the ">" closing of the tag. The ignore case will help when you are not sure that the element is written in Upper or Lower case.
这将搜索字符串中以“<”开头的部分,如果标签以“/”结尾,最后以“>”结尾,则在“div/p/br”之间包含一些文本。当您不确定元素是大写还是小写时,忽略大小写会有所帮助。