How to write regex for this javascript string -
how write string below
"(22.0796251, 82.13914120000004),36", "(22.744108, 77.73696700000005),48",...and on like this:
(22.0796251, 82.13914120000004) 36 (22.744108, 77.73696700000005) 48 ...and on.................. .. how using regex in javscript ?
my try this:
substring = test.split(','); where test contains data formatted. wrong
you should use ability of split split on regular expressions , keep them in results. this, put capturing group in regexp. in case, "split" on things in double quote marks:
pieces = test.split(/(".*?")/) ^^^^^^^ capture group // ["", ""(22.0796251, 82.13914120000004),36"", ", ", ""(22.744108, 77.73696700000005),48"", ""] the question mark make sure doesn't eat characters through last quote in input. makes * quantifier "non-greedy".
now rid of junk (empty strings , ", "):
pieces = pieces . filter (function(seg) { return !/^[, ]*$/.test(seg); }) // ["(22.0796251, 82.13914120000004),36", "(22.744108, 77.73696700000005),48"] next can break down each piece regexp, in
arrays = pieces . map(function(piece) { return piece.match(/(.*), (.*)/).slice(1); }); // [["(22.0796251, 82.13914120000004)", "36"], ["(22.744108, 87.73696700000005)", "48"]] the slice rid of first element of array returned match, entire match , don't need that.
now print out arrays, split elements further, or whatever else want it.
Comments
Post a Comment