node.js - using regex in nodejs filtering multiple delimiters -
i have multiple emails in string multiple delimiters ,;:/|\""
.
im trying slip , add in array.
im there small issue coming know wrong in regex.
node js code:
var x = "mmmm lll\"kkkk\jjj/iiii,hhhh:gggg+ffff-eee+dddd;cccc|bbbb:aaaa"; var separators = [' ', '\\\+', '-', ';', '"', '\\|','//', '\\|', '\\\(', '\\\)', '\\*', '/', ':', '\\\?']; console.log(separators.join('|')); var tokens = x.split(new regexp(separators.join('|'), 'g')); console.log(tokens);
here output:
|\+|-|;|"|\||//|\||\(|\)|\*|/|:|\? [ 'mmmm@gmail.com', 'lll@gmail.com',
'kkkk@gmail.comjjj@gmail.com', 'iiii@gmail.com,hhhh@gmail.com',
'gggg@gmail.com', 'ffff@gmail.com', 'eee@gmail.com', 'dddd@gmail.com', 'cccc@gmail.com', 'bbbb@gmail.com', 'aaaa@gmail.com' ]
[1] can add ,
list of separators.
[2] when declaring string x
, assumes has escapes comes after \
, if display x
see without \
delimiter. change delimiter \\
.
[3] add delimiter \
also.
new updated code working in nodejs:
var x = "mmmm lll\"kkkk\\jjj/iiii,hhhh:gggg+ffff-eee+dddd;cccc|bbbb:aaaa"; \\ ^^^ <- note this, otherwise it's trying escape `j` console.log(x) var separators = [' ', '\\\+', '-', ';', '"', '\\|','//', '\\|', '\\\(', '\\\)', '\\*', '/', ':', '\\\?', ',', '\\\\\+']; console.log(separators.join('|')); var tokens = x.split(new regexp(separators.join('|'), 'g')); console.log(tokens);
Comments
Post a Comment