简体   繁体   中英

javascript how to add comma between special characters

I have a string which I would like to use in an array. My string looks like this:

[[30.251449,-97.893596][30.406576,-98.057163][30.370589,-97.946591][30.362589,-97.981294]]

I would like it to look like this:

[[30.251449,-97.893596],[30.406576,-98.057163],[30.370589,-97.946591],[30.362589,-97.981294]]

thanks

String.replace can do that

str = str.replace(/\]\[/g, '],[');

FIDDLE

You can replace all instances of "][" with "], [", like this:

var myString = "[[30.251449,-97.893596][30.406576,-98.057163][30.370589,-97.946591][30.362589,-97.981294]];"
var validJSON = myString.replace(/\]\[/g, "], [");

// To get actual arrays:
var arrays = JSON.parse(validJSON);

Edit: previous way would only replace one occurrence

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