我在使用方括号分割数组来分割文本时遇到问题。如果方括号内没有文字,那么它就不会被捕获。代码在JavaScript中,如下例所示:
var text = 'This note is created on [date] by [admin;operator] for []'
var myArray = text.match(/\[([^[]+)\]/g);
console.log(myArray);
,结果是
["[date]", "[admin;operator]"]
但我想要的是
["[date]", "[admin;operator]", []]
如何改进我的模式以获得空方括号([])?
答案 0 :(得分:1)
只需将<Grid Width="500" Height="375">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image x:Name="image" Grid.RowSpan="2" Source="C:\Users\Clement\Desktop\test_image.png" />
<Grid Margin="0,0,0,166" Grid.RowSpan="2">
<Image Width="{Binding ActualWidth, ElementName=image}"
Height="{Binding ActualHeight, ElementName=image}"
HorizontalAlignment="Center" VerticalAlignment="Top">
<Image.Source>
<FormatConvertedBitmap Source="{Binding Source, ElementName=image}"
DestinationFormat="Gray8"/>
</Image.Source>
<Image.OpacityMask>
<ImageBrush ImageSource="C:\Users\Clement\Desktop\test_image.png"/>
</Image.OpacityMask>
</Image>
</Grid>
</Grid>
(1或更多)替换为+
(0或更多)
*
&#13;
答案 1 :(得分:1)
n+
匹配包含至少一个n
n*
匹配包含零次或多次n
var text = 'This note is created on [date] by [admin;operator] for []'
var myArray = text.match(/\[([^[]*)\]/g);
console.log(myArray);