I'm currently recoding some of my PHP code into javascript code for my node.js server.
My PHP-Reqex looks like this:
$timevalue = "W5D4H7M34S12";
$aSplitted = preg_split('#(?<=\d)(?=[a-z])#i', $timevalue);
The result for $aSplitted looks like this:
["W5","D4","H7","M34","S12"]
This is what I tried in javascript:
var aSplitted = timevalue.split(/(?<=\d)(?=[a-z])#i/g);
Or
var aSplitted = timevalue.split((?<=\d)(?=[a-z])#i);
Or
var aSplitted = timevalue.split('(?<=\d)(?=[a-z])#i');
but it wont work.
I've also tried something like this:
var aSplitted = timevalue.split(/[^A-Za-z]/);
But it only gives me the chars without the numbers after.
Maybe some of you know the solution?
split
accepts the delimiter, and splits the string by the delimiter. You are looking for match
.
In the JavaScript syntax, #
should be replaced with /
. But you've combined the PHP and JS syntax, not valid.
Lookbehinds are not supported by JS
Just simplify it.
To get the matches, you should use
var timevalue = "W5D4H7M34S12";
var aSplitted = timevalue.match(/[a-z]\d+/ig);
document.write(JSON.stringify(aSplitted))