Regex to disregard certain characters in an email

63 views Asked by At

I need your help with extracting correct email with a regular expression. Here is a raw HTML:

  </a>
  <script>
      var XutJPVbvZ = 'sales@some##thing.com';
      var XutJPVbvZ = XutJPVbvZ.split('#');
      document.getElementById('XutJPVbvZ').innerHTML = 
           XutJPVbvZ[0]+XutJPVbvZ[2]+XutJPVbvZ[1]; 
      document.getElementById('XutJPVbvZ').href = 
           'mailto:'+XutJPVbvZ[0]+XutJPVbvZ[2]+XutJPVbvZ[1];
  </script>
</div>

I match an email with this expression:

[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+[a-zA-Z]+[a-zA-Z0-9-_.]+

What I get is this: sales@some##thing.com

How to make regex return an email without two # special characters in the middle of this email address?

Thanks

1

There are 1 answers

0
wp78de On

I do not fully get your question but can answer anyway: use regex replace with capturing groups like shown in the Javascript code below:

const regex = /([a-zA-Z0-9-_.]+)@([a-zA-Z0-9-_.]+)\#\#([a-zA-Z]+)([a-zA-Z0-9-_.]+)/gm;
const str = `sales@some##thing.com`;
const subst = `$1@$2$3$4`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);