Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript equivalent of PHP's sscanf function?

Pretty basic question. I have a string, it will always be in the format of "(45.234235235,55.345345345)" with variable numbers of decimal places. I want to extract both of these numbers. In PHP I would sscanf, but I want to do this in Javascript.

like image 328
Casey Flynn Avatar asked Oct 28 '25 04:10

Casey Flynn


2 Answers

There is nothing like sscanf, but you can use .replace() and .split():

var data = str.replace(/[()\s]+/g, '').split(',');
like image 169
Felix Kling Avatar answered Oct 29 '25 17:10

Felix Kling


Have you heard of the php.js project?

http://phpjs.org/functions/sscanf:886

This is to directly answer your question only. The JavaScript-native way to tackle your problem would be to use regular expressions. See Felix Kling's answer for the proper solution.

like image 37
2 revsAtes Goral Avatar answered Oct 29 '25 19:10

2 revsAtes Goral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!