Rstrip based on regex in Javascript -


i'm trying validate list of email addresses client-side. these addresses separated comma or semi-colon. typically this:

'fake@mail.com; another@mail.com; ' 'myaddress@mail.com, myfriend@mail.com' 

i'm using regular expression split string (i know how validate each email address separately):

var separator = /[;,]+/; var emails = value.split(separator); 

i want able remove last separator if there one. server-side in python i'm doing that:

data = data.rstrip(separator) value_list = re.split(separator, data) 

with separator being desired regexp.

my question is: how do in javascript ? more broadly, how remove last characters of string if match regexp ?

rstrip can done using end line $ symbol in regex .replace() method. have changed regular expression can additionally remove spaces.

var separator = "[; ,]+",     emails = value.replace(new regexp(separator + "$"), "")                   .split(new regexp(separator));  console.log(emails); 

Comments