How do I split this string using JavaScript? -
i have string
(<<b>>+<<10>>)*<<c>>-(<<x>>+<<y>>)
using javascript, fastest way parse into
[b, 10, c, x, y]
var str = '(<<b>>+<<10>>)*<<c>>-(<<x>>+<<y>>) '; var arr = str.match(/<<(.*?)>>/g); // arr ['<<b>>', '<<10>>', '<<c>>', '<<x>>', '<<y>>'] arr = arr.map(function (x) { return x.substring(2, x.length - 2); }); // arr ['b', '10', 'c', 'x', 'y']
or can use exec
capture groups directly:
var regex = /<<(.*?)>>/g; var match; while ((match = regex.exec(str))) { console.log(match[1]); }
this regular expression has benefit can use in string, including other alphanumerical characters, without having them matched automatically. tokens in << >>
matched.
Comments
Post a Comment