即使支架内没有文字,也可以用方括号拆分

时间:2018-02-19 09:27:21

标签: javascript

我在使用方括号分割数组来分割文本时遇到问题。如果方括号内没有文字,那么它就不会被捕获。代码在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]", []]

如何改进我的模式以获得空方括号([])?

2 个答案:

答案 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;
&#13;
*
&#13;
&#13;
&#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);