0

i have multiple emails in string with multiple delimiters ,;:/|\"".

im trying to slip and add in array.

im almost there small issue is coming i know something wrong in my 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 is 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' ]
M.A
  • 448
  • 6
  • 21

1 Answers1

0

[1] You can add a , to your list of separators.

[2] When you are declaring the string x, it assumes that it has to escapes what comes after the \, so if you display x you will see it without the \ delimiter. So change the delimiter to \\.

[3] Add a delimiter for the \ 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 to escape `j`
console.log(x)
var separators = [' ', '\\\+', '-',  ';', '"',  '\\|','//', '\\|',  '\\\(', '\\\)', '\\*', '/', ':', '\\\?', ',', '\\\\\+'];

console.log(separators.join('|'));
var tokens = x.split(new RegExp(separators.join('|'), 'g'));
console.log(tokens);
TheChetan
  • 4,440
  • 3
  • 32
  • 41
  • thanks for your quick reply.. i can not change string ("mmmm lll\"kkkk\\jjj/iiii,hhhh:gggg+ffff-eee+dddd;cccc|bbbb:aaaa) this is not in my hand ... thats why i need to do something in regex code ... – M.A Sep 14 '17 at 03:55
  • The problem is that js doesn't consider \ to be in the string. See `x.indexOf('\\')` – TheChetan Sep 14 '17 at 04:19
  • See: https://stackoverflow.com/a/3903661/4110233, you need \\ everywhere instead of a single one as it is a special character in js. – TheChetan Sep 14 '17 at 04:24