S_B
asked on
Replace . by [dot] in emails which are placed within text via Regular Expression
In Java i want to replace the '.' in an email which is placed within a text with [dot] and i want to do this via a regular expression just by string.replaceAll("regex", "[dot]")
the tricky part is that the emailaddress ist placed within other (html)text and the dots not belonging to an email must be preserved.
exampletext::
any text. <anytag>...<anytag>
firstname.lastname@domain. com
more text, more tags etc.
After parsing the text should look like this:
any text. <anytag>...<anytag>
firstname[dot]lastname@dom ain[dot]co m
more text, more tags etc.
Any help appreciated!
the tricky part is that the emailaddress ist placed within other (html)text and the dots not belonging to an email must be preserved.
exampletext::
any text. <anytag>...<anytag>
firstname.lastname@domain.
more text, more tags etc.
After parsing the text should look like this:
any text. <anytag>...<anytag>
firstname[dot]lastname@dom
more text, more tags etc.
Any help appreciated!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Like this:
<script>
var myHtml = "<some>Text and Email: adr0.adr1.adr2@ee.com <br>\n more.test: adr0.adr1.adr2@ee.com <br> more.test.";
var dotHtml = myHtml.replace(/([\w\-]+\. )*\w+\@([\ w\-]+\.)*\ w+/g,funct ion(m){ret urn m.replace(/\./g,"[dot]")}) ;
alert(dotHtml);
</script>
<script>
var myHtml = "<some>Text and Email: adr0.adr1.adr2@ee.com <br>\n more.test: adr0.adr1.adr2@ee.com <br> more.test.";
var dotHtml = myHtml.replace(/([\w\-]+\.
alert(dotHtml);
</script>
Or even simplified to:
<script>
var myHtml = "<some>Text and Email: adr0.adr1.adr2@ee.com <br>\n more.test: adr0.adr1.adr2@ee.com <br> more.test.";
var dotHtml = myHtml.replace(/[\w\-\.]+\ @[\w\-\.]+ /g,functio n(m){retur n m.replace(/\./g,"[dot]")}) ;
alert(dotHtml);
</script>
<script>
var myHtml = "<some>Text and Email: adr0.adr1.adr2@ee.com <br>\n more.test: adr0.adr1.adr2@ee.com <br> more.test.";
var dotHtml = myHtml.replace(/[\w\-\.]+\
alert(dotHtml);
</script>
ASKER
@amit_g: worked for me!
@Zvonko: Your solution is JavaScript and i need Java...
@Zvonko: Your solution is JavaScript and i need Java...
a.b.c@d.e.f.g
a@b.c.d
If this is an effort to fool SPAM e-mail address harvesting bots, they've figured out the [dot] trick and it won't fool them. But, if you must do this, I would first start with a pattern that would match most valid SMTP e-mail address formats, such as:
http://regexlib.com/REDetails.aspx?regexp_id=1336
Then loop through the matches, using a simple replace "." with "[dot]" and "@" with "[at]" if desired.