我尝试使用REGEX在文本中识别bbcodes
。
我有以下文字:
Lorem ipsum dolor sit amet, [color] consectetur adipisicing el it labore et [color=red]dolore magna aliqua[/color] minim veniam.
目前我正在使用这种模式:
/\[([a-z0-9]+).+?\[\/\1\]/i
但它抓住了这个:
[color] consectetur adipisicing el it labore et [color=red]dolore magna aliqua[/color]
而不是:
[color=red]dolore magna aliqua[/color]
我想到了两个解决方案,但我不知道如何让它发挥作用:
[b]this [b] won't be allowed[/b]
; 感谢您的帮助,
JG
答案 0 :(得分:1)
你的正则表达式找到最左边的.totalWrapper {
width: 964px;
height: auto;
margin-bottom: 250px;
box-sizing: border-box;
padding: 0;
position: relative;
}
.wrapper1 {
width: 964px;
height: 200px;
position: absolute;
left: 50%;
margin-left: -50%;
margin-top: -10px;
}
.shrink-wrap {
width: 100vw;
height: 100%;
top: -5%;
position: relative;
overflow: visible;
display: inline-block;
}
.subSubHeaderImage {
width: 100vw;
height: 100%;
left: 50%;
right: 50%;
margin-left: -50vw;
margin-right: -50vw;
background: url(http://localhost/wordpress/wp-content/uploads/2017/04/sandwichmaaler.png) center no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-ms-background-size: cover;
-o-background-size: cover;
background-size: cover;
position: absolute;
overflow: visible;
}
.subSubHeaderImageTekst h1 {
width: 100%;
top: 35px;
align-items: center;
position: absolute;
font-family: "Roboto Slab", sans-serif;
text-align: center;
font-size: 36px;
color: #fff;
z-index: ;
}
.subSubHeaderImageTekst p {
width: 100%;
position: absolute;
top: 95px;
color: #a8adb1;
line-height: 26px;
font-family: "Roboto Slab", sans-serif;
text-align: center;
font-weight: 300;
font-size: 18px;
}
.wrapper2 {
width: 964px;
height: auto;
margin: 0;
padding: 30px 0;
position: absolute;
z-index: 1;
top: 190px;
}
.kolonne1 {
float: left;
width: 100%;
height: auto;
margin-top: 40px;
-webkit-column-count: 2;
-moz-column-count: 2;
column-count: 2;
-webkit-column-gap: 60px;
-moz-column-gap: 60px;
column-gap: 60px;
}
.kolonne1 img {
height: auto;
margin-top: -20px;
width: 85%;
}
后跟BBtag,然后<div class="totalWrapper">
<div class="wrapper1">
<div class="shrink-wrap">
<div class="subSubHeaderImage">
</div>
<!--end of .subSubHeaderImage-->
<div class="subSubHeaderImageTekst">
<h1>texttexttexttext</h1>
<p>texttexttexttextt</p>
</div>
<!--end of .subSubHeaderImageTekst-->
</div>
<!--end of .shrink-wrap-->
</div>
<!--end of .wrapper1-->
<div class="wrapper2">
<div class="kolonne1">
texttexttexttexttexttexttext
<img src="http://localhost/wordpress/wp-content/uploads/2017/04/burger_lille-300x200.png" alt="burger" width="350" height="233" />
</div>
<!--end of .kolonne1-->
</div>
<!--end of .wrapper2-->
<div class="push">
</div>
<!--end of .push-->
</div>
<!--end of .totalWrapper-->
匹配除了换行符以外的任何1个字符,尽可能少,但需要尽可能多的找到最左边的字符串[
。
您需要确保在前往结束标记的途中与开始标记不匹配:
.+?
请参阅regex demo
它与\[([a-z0-9]+)(?:(?!\[\1\b).)+?\[\/\1\]
几乎相同,可能更具可读性,但效率较低。
<强>详情:
[/<CLOSE_TAG>]
- 一个开放式括号\[([a-z0-9]+)[^\[]*(?:\[(?!\1\b)[^\[]*)*?\[\/\1\]
- 第1组(标记名称):1+个字母数字符号\[
- 除([a-z0-9]+)
[^\[]*
- 0+序列(尽可能少)匹配
[
- (?:\[(?!\1\b)[^\[]*)*?
未跟随第1组文字作为整个单词\[(?!\1\b)
- 除[
[^\[]*
- [
\[
- [
\/
- 第1组文字/
- \1
。