I want to break up [] into separate groups, how to do this?
- Give an example of input and output. - user218976
- oneYou did not quite understand me correctly - I meant give an example a few lines that you submit to the input of a regular expression, and what you expect at the output for these lines. - user218976
- int, string, bool, float, array, can also be objects, but there can be arrays from all these types int [], int [] [] ... - frago
- oneWrite a syntax parsing? Regulars for this bad fit. And finally, provide real examples in the format: at the entrance it is ... It should work out ... - Total Pusher
|
1 answer
If "in the forehead," then simply add redundant groups, because the number is unknown (non-found groups are simply not displayed). This is terribly wrong, but it works:
var sText = 'int[Вторая][3][][Последняя]'; var rExp = /([A-Za-z0-9]+)(\[.*?\])(\[.*?\])?(\[.*?\])?(\[.*?\])?(\[.*?\])?(\[.*?\])?(\[.*?\])?/; var sResult = sText.replace(rExp, '$1 $2 $3 $4 $5 $6 $7'); console.log(sResult); You can also get groups from an array using match . In this case, non-existent indices will have the value undefined :
var sText = 'int[Вторая][3][][Последняя]'; var rExp = /([A-Za-z0-9]+)(\[.*?\])(\[.*?\])?(\[.*?\])?(\[.*?\])?(\[.*?\])?(\[.*?\])?(\[.*?\])?/; var sResult = sText.match(rExp); console.log(sResult[1], sResult[3], sResult[10]); |
