简体   繁体   中英

replace dot convention array to square brackets

I have a string that represent array:

context.something.1.nothing  

The regex pattern would be: \\.\\d+ .

How to replace it to the following format:

context.something[1].nothing  

Of course the array can be any length.

You could accomplish it in the following way ....

 var arr = 'context.something.1.nothing'; var newArr = arr.replace(/\\.(\\d+)/, '[$1]'); console.log(newArr); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM