regex - Javascript lookbehind stopped at the first match -
i have parse (in js) e-mail address originally, instead of @ has dot. want show @ instead of dot.
var mail = "name.domain.xx" we have 2 cases:
name contains dots , backslashed:
name\.surname.domain.xxname contains regular characters (non dots)
in this topic found way implement negative lookbehind , did:
mail = mail.replace(/(\\)?\./, function ($0, $1) { return $1?$0:"@"; }); but it's not working because in case (1) finds \., not touch it, , of course stops.
on other end, if use option g, substitute third dot obtaining name.surname@domain@xx
now, there way say: want in whole string want stop in first match? hope explained myself.
cheers
i misunderstood when first answering question. have changed answer.
if don't put /g flag, replace first match, meaning can first punctuation without \ in front of it. second can replace \. belonging user part of email regular punctuation.
var emailsingle = 'myname.domain.com', emaildouble = 'my\\.name\\.another\\.name.domain.com', regat = /([^\\])\./, repat = '$1@', regpunct = /\\./g, reppunct = '.'; emailsingle = emailsingle.replace(regat, repat).replace(regpunct, reppunct); emaildouble = emaildouble.replace(regat, repat).replace(regpunct, reppunct); alert('emailsingle: ' + emailsingle + '\nemaildouble: ' + emaildouble);
Comments
Post a Comment